Output looks good with a local file on my computer, fails the last condition unfortunately.
Recommendation says it counts more instances of the word "world". Why or how?
package com.codegym.task.task19.task1907;
/*
Counting words
*/
import java.io.*;
import java.nio.BufferOverflowException;
public class Solution {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = "";
try{
name = reader.readLine();
}catch (IOException ioe){
}
try (BufferedReader br = new BufferedReader(new FileReader(name))){
String line = "";
int count = 0;
while ((line = br.readLine()) != null){
int index = line.indexOf("world");
while (index != -1){
count++;
line = line.substring(line.indexOf("world") + 5);
index = line.indexOf("world");
}
}
System.out.println(count);
}catch (FileNotFoundException fnfe){
}catch (IOException ioe){
}
}
}