Checked my code on 10 different files. Everything works fine, but the last condition fails.
package com.codegym.task.task19.task1907;
/*
Counting words
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
int count = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = br.readLine();
BufferedReader file = new BufferedReader(new FileReader(fileName));
while (file.ready()) {
String words = file.readLine();
while (words.contains("world")) {
words = words.replaceFirst("world", "*");
count++;
}
}
br.close();
file.close();
System.out.println(count);
}
}