[英]java: about counting in txt file
i want to counting some words in txt file 我想数一些txt文件中的单词
myfile.txt myfile.txt
ABC,xyzwegwegwe
ABC,12312312312
ABC,sdfsdf3sdfs
how can i count the words"ABC"? 我如何计算“ ABC”一词?
output: "ABC" have: 3
输出:
"ABC" have: 3
while (myfile.hasNextLine()) {
line = myfile.nextLine();
lines.add(line);
if(xxxxx){ //if have ABC, words++
words++;
}
}
System.out.print("\"ABC\" have: "+words);
I believe what you are trying to do is (and if it only has one copy of "ABC" per line) 我相信您要尝试做的是(如果每行只有一个“ ABC”副本)
if(line.contains("ABC"))
{
words++;
}
String lineToTest = "ABC , sdq2we9ieorwq , EFG"
if(line.contains("ABC"))
{
words++;
}
if(line.contains("EFG"))
{
words++;
}
Notice that this will not check for dupliates!!! 请注意,这不会检查重复项!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.