Reading chars from a file
Hi, I desperately need help with my program. Im suppose to get a file from the user, and then print out the number of words, lines, and characters in that file, excluding whitespaces for the number of characters. Heres my Code Output and contents of my test file. I cant get the characters to work.
Output:
Code:
run:
What is the file name? essay.txt
Words: 7
Lines: 2
Characters: 0
BUILD SUCCESSFUL (total time: 4 seconds)
Contents of test file:
Code:
Hey, this is a test!
Java Programming.
Code:
Code:
package mat2670;
import java.io.*;
import java.util.*;
public class CheckPaper {
private static int wordCount;
private static int lineCount;
private static int charCount;
public static void main(String[] args) throws FileNotFoundException {
//Set up the scanner to get a file
Scanner console = new Scanner(System.in);
System.out.print("What is the file name? ");
String name = console.nextLine();
Scanner inputLine = new Scanner(new File(name));
File f = new File("essay.txt");
lines(inputLine);
//Print out the # of Words, Lines, and Characters in the file
System.out.println("Words: " + wordCount);
System.out.println("Lines: " + lineCount);
System.out.println("Characters: " + charCount);
}
//Method to count the # of words
private static int words(String input) {
String[] wrds = input.split(" ");
wordCount += wrds.length;
return wordCount;
}
//Method to count the # of characters
private static int chars(String input) {
String[] words = input.split(" ");
int charCount = input.replaceAll("\\s", "").length();
return charCount;
}
//Method to count the # of lines
private static int lines(Scanner input) {
while (input.hasNextLine()) {
String line = input.nextLine();
words(line);
lineCount++;
}
return lineCount;
}
}
Re: Reading chars from a file
do you ever call you char function?
Re: Reading chars from a file
I shouldn't have to? I have the method set up like the other two where its returning the value (charCount in this case), and my other 2 methods are working no problem.
Re: Reading chars from a file
Yes, you have to call it. You call your words function on line 52. Make a call to your chars function and see what happens. I have not tried to compile or run your program, but that's were I would start.
Re: Reading chars from a file
inserted a chars(line); after the words call. still getting same output
Re: Reading chars from a file
That's because your chars function is not coded correctly. I'll give you 1 hint. why are you splitting the input? Do you need to do that? You should be able to find the other issue. for one look at how you did words, whats different about the 2 functions?
Re: Reading chars from a file
Quote:
Originally Posted by
foulkelore
That's because your chars function is not coded correctly. I'll give you 1 hint. why are you splitting the input? Do you need to do that? You should be able to find the other issue. for one look at how you did words, whats different about the 2 functions?
Ok, i see what you were getting at now. Thank you so much, i'm getting the character count now.
Code:
//Method to count the # of characters
private static int chars(String input) {
charCount += input.replaceAll("\\s", "").length();
return charCount;
}