it reads words separated by punctuation marks. Hence the regex \\p{Punct} should do the trick. But I fail the test.
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 int count = 0;
public static void main(String[] args) throws IOException {
//step 1: The program must read the file name from the console (use BufferedReader).
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
//step 2: The BufferedReader used for reading input from the console must be closed after use.
reader.close();
//step 3: The program must read the file's contents (use the FileReader constructor that takes a String argument).
// The file contains words separated by punctuation marks. so it is 1 line? Maybe not.
BufferedReader reader2 = new BufferedReader(new FileReader(fileName));
//in case the file has more than 1 line
StringBuilder content = new StringBuilder();
while (reader2.ready()) {
String line = reader2.readLine();
content.append(line);
}
//step 5: The program must output to the console the number of times the word "world" appears in the file.
//step5.1: spilt the string into separate words
//String[] words = content.toString().split([]);
String[] words = content.toString().split("\\p{Punct}");
for (String word : words) {
if (word.equals("world")) {
count++;
}
}
System.out.println(count);
//step 4: The file input stream (FileReader) must be closed.
reader2.close();
}
}