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
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:
- How to program a CAN-BUS arduino shield to control car windows?Best solution by Arduino
- How to Count total number of Words in PDF?Best solution by Super User
- How to count the number of lines in a file using Bash?Best solution by Stack Overflow
- Is there a program that can play wii games?Best solution by Yahoo! Answers
- How to create this Java program?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.