Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: checking for digit while reading from a file

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default checking for digit while reading from a file

    guys, let's say I have a file that contains

    1
    33
    b s
    4
    10
    f
    a
    120
    5 6

    I want to count the numbers and letters, in my code I'm using the Character.isDigit(String.chartat(0)) to identify the numbers. But you can only use nextInt(); to read the input. this is very straight forward using next(), but how could I do it using nextInt();

    public static void main(String[] args) {
            mixedF();
        }
     
        public static void mixedF() {
            int numbers = 0; 
            int words = 0;
            String ch;      
            try {
                Scanner in = new Scanner(new File("myFile.txt"));
                while(in.hasNext()) {
                    ch = in.next();
                    if(Character.isDigit(ch.charAt(0)))
                        numbers++;
                    else
                        words++;              
                }
     
                System.out.println("numbrers: "+numbers);
                System.out.println("words: "+words);
            }
            catch (FileNotFoundException ex) { ex.getMessage(); }
     
        }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: checking for digit while reading from a file

    I would suggest you read about the scanner class and become familiar with the methods available and their usage. Reading that page will answer your question and perhaps teach you even more useful things.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: checking for digit while reading from a file

    nextInt() will give you an error once it encounters a letter... how would you handle that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: checking for digit while reading from a file

    put the call to nextInt() inside of a try catch block to catch the error of trying to read a non-integer.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: checking for digit while reading from a file

    Quote Originally Posted by Norm View Post
    put the call to nextInt() inside of a try catch block to catch the error of trying to read a non-integer.
    this is my implementation, but I'm getting "imputMisMatchException"

    public static void mixedF() {
            int numbers = 0; 
            int words = 0;
            int num;      
            try {
                Scanner in = new Scanner(new File("myFile.txt"));
                while(in.hasNext()) {
                    try {
                        num = in.nextInt();
                        numbers++; 
                    }
                    catch (Error e) { words++; }              
     
                    }            
     
     
                System.out.println("numbrers: "+numbers);
                System.out.println("words: "+words);
            }

    Exception in thread "main" java.util.InputMismatchException

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: checking for digit while reading from a file

    java.util.InputMismatchException
    What does the API doc say for the method you are using?
    I'd expect that error if the data being read was not was expected. For example a letter instead of digits.

    What error condition did you want the catch block to handle? Use that in the catch block instead of Error.

    Read the API doc for the Error class to see when you should use that class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  2. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  3. Need help with reading from .dat file
    By cyoung in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 26th, 2011, 07:34 AM
  4. Timer digit
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 2nd, 2011, 08:07 PM
  5. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM