0

我正在为我的 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循环无限运行,但我不知道为什么!

4

5 回答 5

3

我将简单地使用 split 如下:

    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++;
       }
    }
于 2012-12-07T21:35:05.267 回答
2

目前我将首先指出您的代码存在一些问题:-

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

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

其次,您正在修改您inputloop itself. 并且您inputloop. 你不应该做这样的事情。相反,您可以使用另一个变量,它将存储您处理的最后一个单词的索引。并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方法on ,它将返回一个包含所有单词的数组,您可以对这些单词进行操作。splitspace

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

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);
于 2012-12-07T21:35:12.397 回答
0

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

于 2012-12-07T21:38:09.180 回答
0

查看子字符串的 javadoc。它从您给它的索引开始,并且包含在内。所以你的 substring 调用总是会给你一个长度至少为 1 的字符串。

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);
于 2012-12-07T21:38:58.440 回答
0

The do-while loop runs indefinitely because that's what's you've set it up to do. Let's simplify this:

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() is always greater than zero. The block which alters input never executes, so input stays the same, and the length of the string input is always greater than 0.

于 2012-12-07T21:36:24.690 回答