Mona Jalal Mona Jalal - 2 years ago 59
Java Question

Why is my word counter sometimes off by one?

Most of the time it works correctly. Rarely it counts off by one. Any guess?

public static int countWords(File file) throws FileNotFoundException, IOException{
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
List<String> strList = new ArrayList<>();

while ((line=br.readLine())!=null){
String[] strArray= line.split("\\s+");
for (int i=0; i<strArray.length;i++){
return strList.size();


Particularly in the example below it gives 3 instead of 2:


Answer Source

If you are using Java 8, you can use Streams and filter what you consider as a "word". For example:

    List<String> l = Files.lines(Paths.get("files/input.txt")) // Read all lines of your input text
            .flatMap(s->Stream.of(s.split("\\s+"))) // Split each line by white spaces
            .filter(s->s.matches("\\w")) // Keep only the "words" (you can change here as you want)
            .collect(Collectors.toList()); // Put the stream in a List

In this specific case, it will output [k].

You can of course do the same in Java 7 by adapting your code and add this condition in your for loop:

    strList.add(strArray[i]); // Keep only the "words" - again, use your own criteria

It is just more cumbersome.

I hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download