菜鸟求助!
初学者,自己有琢磨了一下!
但是也不是很懂!
希望高手帮忙下面的编程
/**
* Count the number of occurrences of a particular word in a given string.
* For example, wordCount("The damsel then ate the dragon.", "the") returns 1.
* [Neither "The" nor "then" match "the", because capital letters are 
* different from lower case letters and only whole words match.]

* @param fullString the string in which occurrences of word are to be counted
* @param targetWord the word whose occurrences are to be counted
* @return the number of times "word" occurs as a separate word

* NOTE: This method should make use of the StringTokenizer class.
*/
/*
* HINTS:
* A while loop is most appropriate for this method. 
* (while loops are used when you do not know in advance how many times a loop will repeat)

* StringTokenizer is another class in the project. Open it and note 
* that it has TWO methods. If you're unsure how to use them, try creating
* a StringTokenizer object manually and calling methods on it to see how it 
* works.

* Remember when you compare two Strings you must use .equals
*/
public static int wordCount(String fullString, String targetWord) {
return 0;// *** REPLACE THIS LINE WITH YOUR OWN CODE ***
}
不太明白这里的要求!
高手帮忙! 

解决方案 »

  1.   

    计算一个targetWord的在fullString出现的次数wordCount("hellho","h");返回2
      

  2.   

    让你编写一个函数,用while循环,计算targetWord在fullString中出现的次数public static int wordCount(String fullString, String targetWord) { 
    return 0;// 在这里写你的代码 

      

  3.   

    注意得用到StringTokenizer这个类
      

  4.   

    StringTokenizer 是出于兼容性的原因而被保留的遗留类(虽然在新代码中并不鼓励使用它)。建议所有寻求此功能的人使用 String 的 split 方法或 java.util.regex 包。 你还是找本比较新的中文书来学习一下吧
      

  5.   

    public static int wordCount(String fullString, String targetWord) {
            int count = 0;        StringTokenizer st = new StringTokenizer(fullString);        while (st.hasMoreTokens()) {
                if (targetWord.equals(st.nextToken())) {
                    count++;
                }
                continue;
            }        return count;
        }