简体   繁体   English

文字分析Word Counter Java

[英]Text analysis Word Counter Java

I need to write code that reads and does a text analysis of a file. 我需要编写读取和执行文件文本分析的代码。 One of the things it needs to do is to count how many words there are in the file. 它需要做的一件事是计算文件中有多少个单词。 I wrote a method countWords , but when I run the program it returns 0. The text file I am using contains the following: 我写了一个方法countWords ,但是当我运行程序时它返回0。我正在使用的文本文件包含以下内容:

Ask not what your country can do for you ask what you can do for your country 不要问你的国家能为你做什么?

So it clearly should return 17 and not 0. What did I do wrong? 因此,它显然应该返回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);


}

try this? 尝试这个?

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);
}

Declare a new scanner for your count words method, the problem lies under s.next(); 为您的计数词方法声明一个新的扫描器,问题出在s.next();下。 it reads the next word in your buffer and discard the previous ones, so after you called your longest word method, the scanner buffer has been used up. 它会读取缓冲区中的下一个单词并丢弃前一个单词,因此在调用最长单词方法之后,扫描程序缓冲区已用完。

You already read the scanner and reading it again. 您已经阅读了扫描仪,然后再次阅读。 just create another scanner to use in count words method 只需创建另一个扫描仪以数字法使用

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

before 之前

 countWords(fileScanner);

Hope this helps. 希望这可以帮助。

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

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