繁体   English   中英

文字分析Word Counter Java

[英]Text analysis Word Counter Java

我需要编写读取和执行文件文本分析的代码。 它需要做的一件事是计算文件中有多少个单词。 我写了一个方法countWords ,但是当我运行程序时它返回0。我正在使用的文本文件包含以下内容:

不要问你的国家能为你做什么?

因此,它显然应该返回17而不是0。我做错了什么?

public class TextAnalysis {

public static void main (String [] args) throws IOException {
    File in01 = new File("a5_testfiles/in01.txt");
    Scanner fileScanner = new Scanner(in01);

    System.out.println("TEXT FILE STATISTICS");
    System.out.println("--------------------");
    System.out.println("Length of the longest word: " + longestWord(fileScanner));
    System.out.println("Number of words in file wordlist: " );
    countWords(fileScanner);


}

public static String longestWord (Scanner s) {
    String longest = "";
    while (s.hasNext()) {
        String word = s.next();
        if (word.length() > longest.length()) {
            longest = word;
        }
    }

    return (longest.length() + " " + "(\"" + longest + "\")");
}

public static void countWords (Scanner s) throws IOException {
    int count = 0;

        while(s.hasNext()) {
            String word = s.next();
                count++;
        }

    System.out.println(count);


}

尝试这个?

void countWords()
{
          String temp;
          File path = new File("c:/Bala/");//give ur path
          File file = new File(path, "Bala.txt");//give ur filename
          FileReader fr = new FileReader(file);
          char cbuf[] = new char[(int) file.length()];
          fr.read(cbuf);
          temp = new String(cbuf);
          String count[]=test.split("\\s");
          System.out.println("Count:"+t.length);
}

为您的计数词方法声明一个新的扫描器,问题出在s.next();下。 它会读取缓冲区中的下一个单词并丢弃前一个单词,因此在调用最长单词方法之后,扫描程序缓冲区已用完。

您已经阅读了扫描仪,然后再次阅读。 只需创建另一个扫描仪以数字法使用

 fileScanner = new Scanner(<your file object>);

之前

 countWords(fileScanner);

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM