Results 1 to 9 of 9
  1. #1
    joelamos is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Troube Looping "try/catch"

    When I run the below method and intentionally input a letter when a number is expected, I get a repeating loop. Why?

    Java Code:
    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;
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Troube Looping "try/catch"

    The nextInt() method fails and doesn't read anything from the input stream; so the next time you try to read an int, the same offending character is read and the method throws an exception; and again and again and again. A cure is to read (and skip) the entire current line in the catch part.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    joelamos is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Troube Looping "try/catch"

    What do you mean that the same offending character is read? The second time, shouldn't the Scanner re-prompt for input to get a value for genericInt?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Troube Looping "try/catch"

    Quote Originally Posted by joelamos View Post
    What do you mean that the same offending character is read?
    Exactly as I wrote: the offending character isn't read and stays in the input buffer; it'll be read again by the nextInt() method ad nauseam, no matter how much you prompt for new input.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    joelamos is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Troube Looping "try/catch"

    Quote Originally Posted by JosAH View Post
    A cure is to read (and skip) the entire current line in the catch part.
    Sorry for being a bother, but I also don't understand what you mean by read and skip. Also, it's necessary that I get an int value for genericInt via the user.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Troube Looping "try/catch"

    Quote Originally Posted by joelamos View Post
    Sorry for being a bother, but I also don't understand what you mean by read and skip. Also, it's necessary that I get an int value for genericInt via the user.
    Fine, but what happens when the user types "foo" or another non-number is that the nextInt() method refuses to read those characters and throws an exception. In your catch block you want to get rid of those characters from the input buffer; simply read the entire line (with the offending character(s)) and forget about the line; only then you try to try a nextInt() call so that it reads fresh characters.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    joelamos is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Troube Looping "try/catch"

    I'm not exactly sure how to go about reading the line with the offending characters. How do I access it?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Troube Looping "try/catch"

    Quote Originally Posted by joelamos View Post
    I'm not exactly sure how to go about reading the line with the offending characters. How do I access it?
    Read the API documentation for the Scanner class; it has a nextLine() method that reads an entire line for you. Read a line and forget about it because it contains the offending character(s) which you already know because your code ended up in the catch block.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    joelamos is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Troube Looping "try/catch"

    Thanks so much for all your help. I finally got it working! :)
    Last edited by joelamos; 04-21-2012 at 11:51 PM.

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  2. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  3. Replies: 4
    Last Post: 10-07-2011, 09:33 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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