What I am doing wrong?
1. I append all the file content to a string builder.
2. I replace all the spaces of the builder.toString with a punctuation.
3. I split the string using the "\\p{Punct}" regex into an aray.
4. I compare all the resulting string with the "world" and increment when I have a match.
Can't pass the final task.
It says: Determine why the program didn't find all instances of the word "world" in the file.
package com.codegym.task.task19.task1907;
/*
Counting words
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String fileName= reader.readLine();
reader.close();
BufferedReader reader1=new BufferedReader(new FileReader(fileName));
String answer="world";
String data;
StringBuilder fileContend=new StringBuilder();
while((data= reader1.readLine())!=null){
fileContend.append(data);
}
reader1.close();
String allFileContent=fileContend.toString();
String allFileNoSpace=allFileContent.replaceAll(" ", ".");
String[] array=allFileNoSpace.split("\\p{Punct}");
int counter=0;
for(String s:array){
if(s.equalsIgnoreCase(answer)){
counter++;
}
}
System.out.println(counter);
}
}