Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By connorlm3

Thread: Reading chars from a file

  1. #1
    connorlm3 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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)
    Contents of test file:
    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;
        }
    }

  2. #2
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Reading chars from a file

    do you ever call you char function?

  3. #3
    connorlm3 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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.

  4. #4
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default 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.

  5. #5
    connorlm3 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Reading chars from a file

    inserted a chars(line); after the words call. still getting same output

  6. #6
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default 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?

  7. #7
    connorlm3 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: Reading chars from a file

    Quote Originally Posted by foulkelore View Post
    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.
    Java Code:
    //Method to count the # of characters
    
        private static int chars(String input) {
            charCount += input.replaceAll("\\s", "").length();
    
            return charCount;
        }
    foulkelore likes this.

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 02:37 AM
  2. Replies: 2
    Last Post: 07-21-2011, 04:46 AM
  3. Deleting specific chars from a file
    By javauserjava in forum New To Java
    Replies: 10
    Last Post: 04-03-2011, 06:25 PM
  4. Replies: 1
    Last Post: 02-07-2011, 09:22 PM
  5. Problem when reading non ascii chars
    By ajay.madamala in forum New To Java
    Replies: 1
    Last Post: 04-18-2009, 05:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •