Two Laptop Bag*
The moose likes Beginning Java and the fly likes Exception Handling using scanner to get user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling using scanner to get user input" Watch "Exception Handling using scanner to get user input" New topic

Exception Handling using scanner to get user input

Ben Alex Barreto

Joined: May 10, 2011
Posts: 15

Hi everyone,

So, a little background on the situation, I wrote something the other day to call a few powershell scripts from a java application, and ended up stripping out all the commands from the powershell script and calling them from java, which made the who process A LOT faster. What would take about 2 hours is now done in about 15 minutes. Anyway, a co-worker saw the code and asked me to compile it and if possible, give him a jar that he would run on his computer to do the same thing (for all my intents and purposes, I was just running it from eclipse).

I began writing a little menu for it, so that he can edit some settings, view them, and then start the process. While I was writing the menu and running some tests on it, I realized I was getting a very annoying bug when handling InputMismatchException from the scanner. I catch the exception, but then when it loops through the options again, the System.out.println I used in the catch block is printed again (like the exception is being thrown again) and it gets into an infinite loop, since the InputMismatchException is thrown over and over again. The pause you see in the catch block is just so I could stop the process in time to see what was going on, otherwise it would print things to the console way too fast.

This is such noobish question/issue, but I was hoping someone could give me some light I'm not a developer (I wish! haha) but I do write some java every now and then when I see it could automate certain things and make our job easier. Anyway, I hope you guys can help! In advance, thank you!!

Menu that's giving me grief (ps. this is not done at all! I kinda stopped in the middle of it, because I am still trying to figure out this exception handling issue).

Settings class, just in case you would like to run the code

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
It looks like the problem is that you're calling nextInt() repeatedly in the loop, without giving it any other exit. When the stream encounters something that's not a number, it throws the exception - but it also resets its own state so that it's as if the nextInt() call was never made, except for the exception being thrown. Which means after you catch the exception, log it, and go back to the beginning of the loop again... that non-int input is still in the scanner, waiting to be parsed. So when you call nextInt() again, the exception gets thrown again. You need to call something other than nextInt() to skip past the bad input - nextLine() might be a good option. You can also use a method like hasNextInt() to test if something is an int before you try to consume it, thereby avoiding the exception.
Ben Alex Barreto

Joined: May 10, 2011
Posts: 15

Mike, you nailed it dead on. Thanks you so much! You're the man!
wood burning stoves
subject: Exception Handling using scanner to get user input