0

我想计算 txt 文件
myfile.txt中的一些单词

ABC,xyzwegwegwe
ABC,12312312312
ABC,sdfsdf3sdfs


我怎么数“ABC”这个词?
输出:"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);
4

1 回答 1

3

我相信你想要做的是(如果每行只有一份“ABC”)

if(line.contains("ABC"))
{
   words++;
}


String lineToTest = "ABC , sdq2we9ieorwq , EFG"

if(line.contains("ABC"))
{
   words++;
}

if(line.contains("EFG"))
{
  words++;
}

请注意,这不会检查重复!

于 2012-04-21T04:14:28.247 回答