Jump to content


Check out our Community Blogs

travy92

Member Since 02 Sep 2007
Offline Last Active Sep 30 2015 08:17 PM
-----

Topics I've Started

Scanner problems

13 March 2011 - 06:25 AM

Hey everyone, I'm having trouble getting the scanner to work for user input.
Basically what's happening is if I input something other than an integer for:

Age = inputVariable.nextInt();

It will give me this error:
java.util.InputMismatchException
.

This is what I have so far:
try { 
		// Get Age
		System.out.println("Age: ");
		Age = inputVariable.nextInt();
		} catch (Exception e) {
			Age = 0;
			inputVariable.reset();
			System.out.println("You have not entered a valid age, please try again.");
                        Age = inputVariable.nextInt();
			System.out.println("Age: ");
       }

I've added Age=0 and inputVariable.reset to try and clear it for re-input but it does not work. I just receive the same error once it gets to the second user input for Age.

So can someone tell me how to clear the scanner for another input?

Thanks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download