Я довольно новичок в java. Я хочу подсчитать количество слов в определенной строке. Пока я могу только считать слова, но не знаю, как считать вхождения.
Есть ли простой способ сделать это?
Scanner file = new Scanner(new FileInputStream("/../output.txt"));
int count = 0;
while (file.hasNextLine()) {
String s = file.nextLine();
count++;
if(s.contains("#AVFC")){
System.out.printf("There are %d words on this line ", s.split("\\s").length-1);
System.out.println(count);
}
}
file.close();
Выход:
There are 4 words on this line 1
There are 8 words on this line 13
There are 3 words on this line 16