I am getting correct results but it won't verify.
Not sure what I am missing,
Verifyer is saying that I am not getting all cases of "world".
package com.codegym.task.task19.task1907;
/*
Counting words
Read a file name from the console.
The file contains words separated by punctuation marks.
Output to the console the number of times the word "world" appears in the file.
Close the streams.
Requirements:
1. The program must read the file name from the console (use BufferedReader).
2. The BufferedReader used for reading input from the console must be closed after use.
3. The program must read the file's contents (use the FileReader constructor that takes a String argument).
4. The file input stream (FileReader) must be closed.
5. The program must output to the console the number of times the word "world" appears in the file.
*/
import java.io.*;
import java.sql.Array;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException, FileNotFoundException {
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
String filename = re.readLine();
//String filename = "C:\\Users\\Alfred\\Documents\\CodeGym Data Files for exercizes\\data.txt";
re.close();
FileReader fileIn = new FileReader(filename);
char[] ch = new char[(int) filename.length()];
int counter = 0;
fileIn.read(ch);
fileIn.close();
String fileString = String.copyValueOf(ch);
String[] words = fileString.split("\\W");
for (int i = 0; i < words.length; i++) {
//System.out.println(words[i]);
if (words[i].equals("world")) {
counter++;
}
}
System.out.println(counter);
}
}