Results 1 to 7 of 7
Thread: Reading chars from a file
- 02-12-2013, 09:51 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
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:
Java Code:run: What is the file name? essay.txt Words: 7 Lines: 2 Characters: 0 BUILD SUCCESSFUL (total time: 4 seconds)
Java Code:Hey, this is a test! Java Programming.
Code:
Java 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; } }
- 02-12-2013, 10:06 PM #2
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 35
- Rep Power
- 0
Re: Reading chars from a file
do you ever call you char function?
- 02-12-2013, 10:25 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
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.
- 02-12-2013, 10:33 PM #4
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 35
- Rep Power
- 0
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.
- 02-12-2013, 10:47 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Reading chars from a file
inserted a chars(line); after the words call. still getting same output
- 02-12-2013, 11:06 PM #6
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 35
- Rep Power
- 0
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?
- 02-12-2013, 11:29 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 6
- Rep Power
- 0
Re: Reading chars from a file
Similar Threads
-
HashMap with Objects, reading from file, writing to file. (note system)
By Java Dude in forum New To JavaReplies: 0Last Post: 12-15-2012, 02:37 AM -
Problem replacing chars in String.replaceAll(), Chars such as (1/2) ½ and (1/4) ¼
By fortwnty420 in forum New To JavaReplies: 2Last Post: 07-21-2011, 04:46 AM -
Deleting specific chars from a file
By javauserjava in forum New To JavaReplies: 10Last Post: 04-03-2011, 06:25 PM -
Intaking String and splitting into chars, returning individual chars as string array
By Gokul138 in forum New To JavaReplies: 1Last Post: 02-07-2011, 09:22 PM -
Problem when reading non ascii chars
By ajay.madamala in forum New To JavaReplies: 1Last Post: 04-18-2009, 05:50 PM
Bookmarks