Java中的WordCount

时间:2012-12-07 21:16:01

标签: java word-count

我正在为我的CS决赛练习一些练习,并且我遇到了这个问题,我必须读取一个字符串,从用户那里获得最小长度,并返回至少包含那么多字母的单词数量。似乎我的代码很好,但它无法打印出答案。任何人都可以帮助我吗?

public class WordCount {



    public static void main (String [] args) {
        System.out.println("Enter a string: "); 
        String input =  IO.readString();


        System.out.println("Enter minimum word length");
        int wordlength = IO.readInt();
        int count = 0 ;
        do  {

            for (int i = 0 ; i < input.length(); i ++) {

                if (input.indexOf(i) == ' ') {

                    String check = input.substring(0, i);
                    if (check.length() >= wordlength) {

                        count++;
                        input = input.substring(i);
                        break;

                    }
                }

                    }

        } while (input.length() > 0);


    System.out.print("Words longer than " + wordlength + " characters: " + count);

    }

}

似乎while循环无限运行,但我无法弄清楚原因!

5 个答案:

答案 0 :(得分:3)

我将简单地使用如下分割:

    System.out.println("Enter minimum word length");
    int wordlength = IO.readInt();
    int count = 0 ;
    //get all words in string array by splitting the input around space
    String[] words = input.split(" ");//assuming words are separated by space

    //now iterate the words, check the length, if word is of desired length or more
    //increase the word counter
    for (int i = 0 ; i < words.length; i ++) {
       if (words[i].length() >= wordlength) {
         count++;
       }
    }

答案 1 :(得分:2)

目前我的代码问题很少,我首先会指出: -

if (input.indexOf(i) == ' ')

在上面的语句中,您应该使用String#charAt方法来获取特定索引处的字符。 String#indexOf方法用于反向过程,即你有一个角色,你想找到它的索引。

其次,您正在修改input内的loop itself。并且您在input的终止条件中使用loop的长度。你不应该做这样的事情。 相反,您可以使用另一个变量,它将存储您处理的最后一个单词的索引。并在index方法中使用substring

第三,这里你真的不需要do while循环。你的for loop本身正在迭代你的所有角色。只需从您的break中删除if,这实际上不是必需的。

因此,您的代码将被修改为: -

int oldIndex = 0;  // to maintain the end index of previous word.
int length = input.length();
for (int i = 0 ; i < length; i ++) {

          if (input.charAt(i) == ' ' || i == length - 1) {

                // If the word is at the end, then probably your first 
                // condition in above `if` would fail, that is why I used a 
                // second condition, which checks the end of string

                // Now for the end of the string, we would need to use a single
                // arguement substring method to get the word till the end.
                // hence the below conditional expression.

                String check = (i == length - 1)? input.substring(oldIndex): 
                                                input.substring(oldIndex, i);

                oldIndex = i + 1;  // Set oldIndex to the next index.

                if (check.length() >= wordlength) {

                    count++;
                    //  input = input.substring(i);  Don't do this
                    // break;   // Don't break too.

                }
           }

}

现在这是对代码的修改,这样你就可以了解你的错误。

但是,您可以轻松获得所需内容。您可以使用String#split方法split space上的String[] words = input.split(" "); // split input string on space for (int i = 0; i < words.length; i++) { // iterate over array if (words[i].length() >= wordLength) { count++; } } System.out.println(count); 字符串,这将返回所有单词的数组,并且您可以对这些单词进行操作。

它是这样的(如果你可以使用它): -

{{1}}

答案 2 :(得分:0)

do-while循环无限期地运行,因为这就是你设置它的目的。让我们简化一下:

string input = "this is an example string";
do
{
     //some logic
     if (input.indexOf(i) == ' ') // this never executes - indexOf(i) returns an int
     {
          //do some stuff with input
     }

}
while (input.length() > 0); 

input.length() 始终大于零。改变input的块永远不会执行,因此input保持不变,字符串input的长度始终大于0。

答案 3 :(得分:0)

您需要使用indexOf(i)来检索位置charAt(i)处的字符,而不是i,循环和逻辑看起来可以用于所述目的。

答案 4 :(得分:0)

查看子字符串的javadoc。它从您给出的索引开始并具有包容性。所以你的子串调用总是给你一个长度至少为一的字符串。

public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

     "unhappy".substring(2) returns "happy"
     "Harbison".substring(3) returns "bison"
     "emptiness".substring(9) returns "" (an empty string)


Parameters:
    beginIndex - the beginning index, inclusive. 
Returns:
    the specified substring. 
Throws:
    IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

尝试这样的事情:

String input = "one two three four five six seven eight nine ten";
    int minLength = 4;
    int count = 0;
    String[] strings = input.split(" ");
    for(String s : strings) {
        if(s.length() >= minLength) {
            ++count;
        }
    }

    System.out.println(count);