Mentor message: Determine why the program found more instances of the word "world" than exist in the file.
package com.codegym.task.task19.task1907;
/*
Counting words
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = br.readLine();
FileReader fr = new FileReader(fileName);
int count = 0;
while(fr.ready()){
char c = (char) fr.read();
if(c == 'w'){
StringBuilder sb = new StringBuilder("w");
for(int i = 0; i<4 && fr.ready();i++)
sb.append((char) fr.read());
if(sb.toString().equals("world")) count++;
}
}
fr.close();
br.close();
System.out.println(count);
}
}