class ll
{
 public static void main(String args[])
{
 String ss="hi he she";
 byte a[]=ss.getBytes();
 int n=a.length;
 int c;
 for(int i=0;i<n;i++)

 if(a[i]=blank)
 c++;
System.out.println("单词个数为:"+c);
 
 }
}
}
有错误,实现不了
应该怎么做呢
现在没分了.不然给你100分

解决方案 »

  1.   

    你的blank是什么? String ss = "hi   he   she ";
    byte blank = 'h';
    byte a[] = ss.getBytes();
    int n = a.length;
    int c=0;
    for (int i = 0; i < n; i++) {   
    if (a[i] == blank){  
    c++;
    }
    System.out.println("单词个数为: " + c); }改成如上
      

  2.   

    可以用到StringTokenizer~
    StringTokenizer ist = new StringTokenizer("hi he she");
    System.out.println("字符串的个数"+ist.countTokens());
      

  3.   

    blank是空格啊
    我想通过空格的个数来统计单词的数目
      

  4.   

    那如下就可以 String ss = "hi he she"; System.out.println(ss.split(" ").length);
      

  5.   

    楼主的方法对于输入两次空格就会出错的
    The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result 
    : { "boo", "and", "foo" } 
    o { "b", "", ":and:f" } 用 Split()這個方法真的太好了
      

  6.   

    ss.split("\\s+");如果空格数不确定 
      

  7.   

    请问各位,在API文档的这个例子我有点不懂为什么还会有一个空格呢o   
     "boo:and:foo ",   
    {   "b ",   " ",   ":and:f "   }   
      

  8.   

    ss.split("\\s+");
    能给我解释一下这个是什意思吗谢谢
      

  9.   

    ss.split( "\\s+ "); 
    能给我解释一下这个是什意思吗谢谢
    =============================
    \\s+是正则表达式 它匹配一个以上的空格。split方法传入一个正则表达式 将ss这个字符串按这个正则表达式进行分解 返回一个String[] 
      

  10.   

    StringTokenizer已经不建议使用了,Sun提倡使用功能更强大的split