Java – finds and counts specific words in a text file

Can someone help me with the code?

For example, test txt:

hi
hola
hey
hi
bye
hoola
hi

If I want to know in test Txt how many times does the "Hi" program have to say "3 repetitions"

I hope you understand what I want. Thank you for your answer

Solution

public int countWord(String word,File file) {
public int countWord(String word,File file) {
int count = 0;
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
    String nextToken = scanner.next();
    if (nextToken.equalsIgnoreCase(word))
    count++;
}
return count;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>