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

Thread: Troube Looping "try/catch"

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Troube Looping "try/catch"

    When I run the below method and intentionally input a letter when a number is expected, I get a never-ending loop that doesn't prompt for more input. Why?

    private static int intInput() //Asks the user for an int and returns the input.
      {
    Scanner genericIntScan = new Scanner(System.in);
        boolean itsAnInt = false;
        int genericInt = 0;
     
       while(itsAnInt==false)
       {
        try
          {
          genericInt = genericIntScan.nextInt();
          itsAnInt = true;
          }
        catch(InputMismatchException ime)
          {
          System.out.println("\nYou're supposed to type in a number. Try again.");
          itsAnInt = false;
          }
        }
       return genericInt;
    }
    Last edited by joelamos; April 21st, 2012 at 03:13 PM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Troube Looping "try/catch"

    Hello joelamos!
    What do you want it to do instead of "repeating"?
    When the exception occurs you have the following statement itsAnInt = false; So the while loop keeps going.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Troube Looping "try/catch"

    What I meant by "repeating" was "never-ending". After it executes the catch lines, shouldn't it go back to "try" and prompt for more input instead of continually repeating the catch lines?
    Last edited by joelamos; April 21st, 2012 at 03:23 PM.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Troube Looping "try/catch"

    Quote Originally Posted by joelamos View Post
    What I meant by "repeating" was "never-ending". After it executes the catch lines, shouldn't it go back to "try" and prompt for more input instead of continually repeating the catch lines?
    You can avoid the infinite loop if you create the Scanner oblect inside the try clause.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Troube Looping "try/catch"

    You need to read the bad input and clear the Scanner's input buffer so the next call to nextInt() will have fresh data. Also you could use one of the has... methods to test if the next input is the correct type.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. "DO EVENTS" while looping
    By anklez in forum Java Theory & Questions
    Replies: 4
    Last Post: December 26th, 2011, 09:02 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 02:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 08:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM