Hi,
I have tried multiple solutions (attached is one) and tested with several local text files - using various combinations - but still fail to verify the last requirement even though I get the same results across my solutions, as well as the Codegym's solution.
The core difference between my solution and codegym's is that I am using
readLine()
rather than read()
Is there something I'm missing?
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 {
String filename;
int count = 0;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
filename = reader.readLine();
}
try (BufferedReader fileReader = new BufferedReader(new FileReader(filename))) {
StringBuilder line = new StringBuilder();
while (fileReader.ready()) {
line.append(fileReader.readLine().replaceAll("\\s", "."));
}
String[] words = line.toString().split("\\p{Punct}");
for (String word : words) {
if (word.equals("world")) count++;
}
}
System.out.println(count);
}
}