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 3 of 3

Thread: InputMismatchException try catch block not working as expected

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool InputMismatchException try catch block not working as expected

    Hi people, I'm new here!

    I have decided to write a small program to learn myself Java. i have created a function that should catch a mismatch exception and respond by asking for the data to be entered again. However, it does not work when I enter purposefully erroneous data.

    When I enter a String (should be an int) the loop goes round constantly. Not sure if its na infinate loop but i always end up resetting the Java Virtual Machine.

    The code:

    public int doPubYear()
        {
            boolean done = false;
            int pubYear = 0;
            while(!done){
                try{
                    System.out.print("Year of publication: ");
                    pubYear = keyboard.nextInt();
                    done = true;
                } catch(InputMismatchException e){
                    System.out.println("Error with input - try again.");
                }            
            }
     
            return pubYear;
        }

    I have :-
    import java.util.Scanner;
    import java.util.InputMismatchException;
    at the top.

    Thanks


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: InputMismatchException try catch block not working as expected

    I have fixed it I needed to use keyboard.nextLine() to discard the input that the user previousley entered.

    My new code:

    public int doPubYear()
        {
            boolean done = false;
            int pubYear = 0;
            while(!done){
                try{
                    System.out.print("Year of publication: ");
                    pubYear = keyboard.nextInt();
                    done = true;
                } catch(InputMismatchException e){
                    System.out.println("Error with input - try again.");
                    keyboard.nextLine();
                }            
            }
     
            return pu

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: InputMismatchException try catch block not working as expected

    Hi Mikhl,

    Yea you found the solution indeed, it should be noted that nextInt() is in fact an interesting function in regards to the fact it does't disregard end line characters etc, which is what causes this seemngly odd behaviour.

    Glad you worked it out

    Chris

Similar Threads

  1. using java.util.InputMismatchException with try
    By itayj in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 26th, 2011, 06:24 PM
  2. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  3. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 10:30 AM
  4. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  5. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 01:02 AM