简体   繁体   中英

java: about counting in txt file

i want to counting some words in txt file
myfile.txt

ABC,xyzwegwegwe
ABC,12312312312
ABC,sdfsdf3sdfs


how can i count the words"ABC"?
output: "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)

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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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