1

这是一个项目,大部分已经完成,但我需要能够计算字符串或文件中的单词数量。我必须使用嵌套的 for 循环,并且必须使用包含单词分隔符的字符串。现在这就是我所拥有的:

public static int wordCounter(String text)
{
    String WORDS_GROUP = ",\n ";
    String text= "This is my sample     text";
    int wordCount=0;
    for(int i=0; i<text.length(); i++){
        for(int j=0; j<WORDS_GROUP.length(); j++){
            if(text.charAt(i)==WORDS_GROUP.charAt(j)){
                wordCount++;
            }
        }
    }
}
4

3 回答 3

0

wordCount仅当最后一个字符不属于时,您才具有增量,WORDS_GROUP否则您最终会得到比实际拥有的更多的单词。例如,当您发现第一个空格后,sample您将拥有wordCount3,但随后又出现另一个空格,您的算法将再次增加,这显然是不正确的。

如果您找到第一个带有空格并执行第一个增量,则可以引入一个新boolean变量。true然后当你发现一个非空白时将其设置为 false 仅当该变量设置为 时才会发生增量false

于 2013-10-27T21:07:28.900 回答
0

您可以维护一个代表您的“状态”的布尔值。您是在一个空格块中(真),还是在一个非空格块中(假)?

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone {
    public static void main(String[] args) throws java.lang.Exception {
        String WORDS_GROUP = ",\n ";
        String text = "This is my sample     text";
        int wordCount = 0;
        boolean previousCharWasSpace = true;
        for (int i = 0; i < text.length(); i++) {
            boolean thisCharIsASpace = false;
            for (int j = 0; j < WORDS_GROUP.length(); j++) {
                if (text.charAt(i) == WORDS_GROUP.charAt(j)) {
                    previousCharWasSpace = true;
                    thisCharIsASpace = true;
                    break;
                }
                System.out.println("char=" + text.charAt(i) + " j=" + j
                        + " previousCharWasSpace=" + previousCharWasSpace);
            }
            if (!thisCharIsASpace && previousCharWasSpace) {
                wordCount++;
                previousCharWasSpace = false;
                System.out.println("char=" + text.charAt(i)
                        + " previousCharWasSpace=" + previousCharWasSpace
                        + " wordCount=" + wordCount);
            }
        }
        System.out.println("wordCount=" + wordCount);
    }
}

输出:

char=T j=0 previousCharWasSpace=true
char=T j=1 previousCharWasSpace=true
char=T j=2 previousCharWasSpace=true
char=T previousCharWasSpace=false wordCount=1
char=h j=0 previousCharWasSpace=false
char=h j=1 previousCharWasSpace=false
char=h j=2 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=false
char=i j=1 previousCharWasSpace=false
char=i j=2 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char=  j=0 previousCharWasSpace=false
char=  j=1 previousCharWasSpace=false
char=i j=0 previousCharWasSpace=true
char=i j=1 previousCharWasSpace=true
char=i j=2 previousCharWasSpace=true
char=i previousCharWasSpace=false wordCount=2
char=s j=0 previousCharWasSpace=false
char=s j=1 previousCharWasSpace=false
char=s j=2 previousCharWasSpace=false
char=  j=0 previousCharWasSpace=false
char=  j=1 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=true
char=m j=1 previousCharWasSpace=true
char=m j=2 previousCharWasSpace=true
char=m previousCharWasSpace=false wordCount=3
char=y j=0 previousCharWasSpace=false
char=y j=1 previousCharWasSpace=false
char=y j=2 previousCharWasSpace=false
char=  j=0 previousCharWasSpace=false
char=  j=1 previousCharWasSpace=false
char=s j=0 previousCharWasSpace=true
char=s j=1 previousCharWasSpace=true
char=s j=2 previousCharWasSpace=true
char=s previousCharWasSpace=false wordCount=4
char=a j=0 previousCharWasSpace=false
char=a j=1 previousCharWasSpace=false
char=a j=2 previousCharWasSpace=false
char=m j=0 previousCharWasSpace=false
char=m j=1 previousCharWasSpace=false
char=m j=2 previousCharWasSpace=false
char=p j=0 previousCharWasSpace=false
char=p j=1 previousCharWasSpace=false
char=p j=2 previousCharWasSpace=false
char=l j=0 previousCharWasSpace=false
char=l j=1 previousCharWasSpace=false
char=l j=2 previousCharWasSpace=false
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char=  j=0 previousCharWasSpace=false
char=  j=1 previousCharWasSpace=false
char=  j=0 previousCharWasSpace=true
char=  j=1 previousCharWasSpace=true
char=  j=0 previousCharWasSpace=true
char=  j=1 previousCharWasSpace=true
char=  j=0 previousCharWasSpace=true
char=  j=1 previousCharWasSpace=true
char=  j=0 previousCharWasSpace=true
char=  j=1 previousCharWasSpace=true
char=t j=0 previousCharWasSpace=true
char=t j=1 previousCharWasSpace=true
char=t j=2 previousCharWasSpace=true
char=t previousCharWasSpace=false wordCount=5
char=e j=0 previousCharWasSpace=false
char=e j=1 previousCharWasSpace=false
char=e j=2 previousCharWasSpace=false
char=x j=0 previousCharWasSpace=false
char=x j=1 previousCharWasSpace=false
char=x j=2 previousCharWasSpace=false
char=t j=0 previousCharWasSpace=false
char=t j=1 previousCharWasSpace=false
char=t j=2 previousCharWasSpace=false
wordCount=5

你可以在这里玩这个:http: //ideone.com/ASEjwB

于 2013-10-27T21:09:48.863 回答
0

如果您绝对需要使用嵌套的 for 循环,您可以让第一个 for 循环遍历文本中的所有行,第二个(嵌套的)for 循环遍历每行的所有单词,然后简单地计算这些单词。

于 2013-10-27T21:05:02.480 回答