简体   繁体   English

计算文本文件中的特定单词-Java

[英]Count specific words from text file - Java

I have a text file and I want to count the total number of specific words I have defined. 我有一个文本文件,我想计算已定义的特定单词的总数。

My code: 我的代码:

    String word1 = "aa";
    String word2 = "bb";

    int wordCount = 0;

     //creating File instance to reference text file in Java
    File text = new File("D:/project/log.txt");

    //Creating Scanner instnace to read File in Java
    Scanner s = new Scanner(text);

    //Reading each line of file using Scanner class
    while (s.hasNext()) {
        totalCount++;
        if (s.next().equals(word1) || s.next().equals(word2)) wordCount++;
    }

    System.out.println("Word count:  " + wordCount);

However, it only counts the number of 'aa's. 但是,它仅计算“ aa”的数量。 It doesn't count the number of 'bb's. 它不计算“ bb”的数量。 what could be the problem? 可能是什么问题呢?

As other said: root of problem that you call next() twice. 就像其他人所说的:您两次调用next()的问题根源。 There just hint how to make your algo easy to extend: 只是提示如何使您的算法易于扩展:

Set<String> words = new HashSet<>(Arrays.asList("aa", "bb"));
...
while (s.hasNext()) {
    totalCount++;
    if (words.contains(s.next())) wordCount++;
}

You are calling s.next() twice in if condition, and each call moves on to the next word. 您在if条件中两次调用s.next() ,每次调用都移至下一个单词。 Change your while loop to 将您的while循环更改为

while (s.hasNext()) {
    totalCount++;
    String word = s.next();
    if (word.equals(word1) || word.equals(word2)) wordCount++;
}

Try this way: 尝试这种方式:

while (s.hasNext()) {
    totalCount++;
    String word = s.next()
    if (word.equals(word1) || word.equals(word2)) wordCount++;
}

Each time you call s.next() , it's finding the next word, so each loop is testing whether one word is "aa" or the next word is "bb". 每次调用s.next() ,它都会找到下一个单词,因此每个循环都在测试一个单词是“ aa”还是下一个单词是“ bb”。 Within the loop, you would need to call s.next() , store the result in a variable, then check that with your two words. 在循环中,您将需要调用s.next() ,将结果存储在变量中,然后使用两个单词进行检查。

Your problem is the you are calling s.next() twice. 您的问题是您两次调用s.next() Each call reads a new token from the input. 每个调用都会从输入中读取一个新令牌。

Change it to : 将其更改为:

while (s.hasNext()) {
    String str = s.next();
    totalCount++;
    if (str.equals(word1) || str.equals(word2)) wordCount++;
}

You call next() two times in your if-condition. 您在if条件中两次调用next()。

try: 尝试:

String word = s.next();

if ( word.equals(word1) ....
    String[] array = new String[]{"String 1", "String 2", "String 3"};

    for(int i=0; i < array.length; i++)
     {
                    System.out.println(array[i]);
                    wordCount=0;
                    while (s.hasNext()) 
                     {
                         totalCount++;
                         if (s.next().equals(array[i])) 
                         wordCount++;
                     }
                     System.out.println("each Word count:  " + wordCount);
     }

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

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