What is the best way to count lines in file?

Java program that can count lines, words, and characters?

  • I am trying to write a code that read a document (text file in this case) and return how many words, characters, and lines there are in the document. In this code, when there is one line, the words and lines count correctly, but when there is more than one line, both values return 0. Here is the code: import java.io.*; import java.util.*; public class countingWords { private static int wordCount; private static int lineCount; public static void main (String[] args) throws FileNotFoundException { Scanner inputLine = new Scanner(new File("essay.txt")); File f = new File("essay.txt"); words(inputLine); lines(inputLine); System.out.println("Words: " + wordCount); System.out.println("Lines: " + lineCount); System.out.println("Characters: " + f.length()); } public static int words(Scanner input) { while (input.hasNext()) { input.next(); wordCount++; } return wordCount; } public static int lines(Scanner input) { while (input.hasNextLine()) { String word = input.nextLine(); lineCount++; } return lineCount; } } I'm not sure why it's doing that. This is how part of the code is in the textbook I am using (Building Java Programs 2nd Edition). Can someone explain why it's doing that?

  • Answer:

    Here's one thing about scanner object, you call words(inputLine) and inside that method it'll call next() till there is no next. So when you call lines(inputLine) there will be no next so lines won't actually do anything but returning 0. If you put lines(inputLine) above words(inputLine) the same thing will happen. you'll get 0 for words and number of lines. I made some modifications and it does the job now: import java.io.*; import java.util.*; public class countingWords { private static int wordCount; private static int lineCount; public static void main (String[] args) throws FileNotFoundException { Scanner inputLine = new Scanner(new File("essay.txt")); File f = new File("essay.txt"); //words(inputLine); lines(inputLine); System.out.println("Words: " + wordCount); System.out.println("Lines: " + lineCount); System.out.println("Characters: " + f.length()); } public static int words(String input) { String[] wrds = input.split(" "); wordCount += wrds.length + 1; return wordCount; } public static int lines(Scanner input) { while (input.hasNextLine()) { String line = input.nextLine(); words(line); lineCount++; } return lineCount; } }

Mike at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

If I call only one of the methods words or lines, I will be getting words and lines. What is the problem? Once we call any of them you will be traversing till end of the stream. Thus second one gives wrong results. Think over how can you solve this

James Bond

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.