Try... Catch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Harukoten
    New Member
    • Oct 2008
    • 3

    #1

    Try... Catch

    Hello everyone, this is my first time posting, so play nice >.>

    I have a loop with a try-catch statement in it that is giving me issues., here is the code for it:
    Code:
    do
    		{
    			System.out.print("Number of courses being entered: ");
    			try
    			{
    				numCourses = userIn.nextInt();
    				failed = false;
    			}
    			catch (InputMismatchException ime)
    			{
    				failed = true;	
    			}
    			if (numCourses < 1 || numCourses > 8 || failed)
    				System.out.println("Invalid Number Entered.  Please enter a valid number between 1 and 8.");
    		}
    		while (numCourses < 1 || numCourses > 8 || failed);
    The idea being, control the range, and check to see if they entered a non-numercial character, thus giving me a InputMismatchEx ception. This is all being done in the console window, btw. My problem is this:
    When I run this, I am given the first prompt, asking me num of courses, fine, I enter an integer, and the rest of the program works just fine. If I enter a letter, or any other non-numerical character, it throws and exception, which it catches, and sets failure to true. It then goes to the if statement, prints the line because failure is true, and then goes to the while(), finds that failure is true, so it goes back to the top, but it DOES NOT ask for another input, it skips the entire try-catch statement, and does the if statement again, giving me an infinite loop.

    So, my question: Why doesn't it redo the try-catch statement once I've been throught the loop once. Why is it skipping try-catch every subsequent run throught the loop, going straight to the if statement?

    Much appreciated, try-catch is still new to me, so it's probaly some property of try-catch I don't know about...

    Thanks again!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    As a matter of fact, it doesn't have anything to do with try ... catch statements:
    I assume you're using a Scanner and a Scanner doesn't read anything when it
    needs to read a number when there isn't any; e.g. it needs a number and you
    type "foo". An exception is throws and nothing is read so the next time you
    read something the same "foo" will still be in the input buffer.

    The solution is simple: when reading fails skip the entire line; read the Scanner
    API documentation how it can read an entire line (which you discard).

    kind regards,

    Jos

    Comment

    • Harukoten
      New Member
      • Oct 2008
      • 3

      #3
      I'll look into that, thank you for the timely reply.

      Hokai, so, I took a look, I don't seem to be able to figuire out what I should be doing, perhaps one more hint in the right direction?
      Last edited by Harukoten; Oct 31 '08, 05:14 AM. Reason: Rawr

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Harukoten
        I'll look into that, thank you for the timely reply.

        Hokai, so, I took a look, I don't seem to be able to figuire out what I should be doing, perhaps one more hint in the right direction?
        If your program executes the statement(s) in the catch clause an exception must
        have been thrown and nothing was read. You have to get rid of the current line
        then. Add a statement there that reads the entire (faulty) line. Read the API
        documentation for the Scanner class.

        kind regards,

        Jos

        Comment

        • Harukoten
          New Member
          • Oct 2008
          • 3

          #5
          AHA!!
          Got it, works exactly as needed. Thank you for the help JosAH.
          And thanks for not giving it away straight up, made me work for my answer, I learned more this way :)

          Comment

          Working...