有人可以帮我写代码吗?
如何在文本文件中搜索任何单词并计算其重复次数?
例如测试.txt:
hi
hola
hey
hi
bye
hoola
hi
如果我想知道 test.txt 中单词“Hi”重复了多少次,程序必须说“3次重复”
希望您能明白我的意思,谢谢您的解答。
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)