0

I prepared following piece of code :

public static void main(String[] args) {
Scanner inChoice = new Scanner(System.in);

while(true)
        {
            try
            {
                System.out.println("--------------------------------------------------------");
                System.out.println("Make your choice :");
                System.out.println("For integer - CHOOSE 1");
                System.out.println("For floating-point numbers - CHOOSE 2");
                intChoice = inChoice.nextInt();
                break;
            }
            catch (InputMismatchException imex)
            {
                System.out.println("You have made a wrong selection. Try again");
                continue;
            }
        }
}

The problem is when I choose something else then integer number (say 'w' for instance). My intention is to give an opportunity to choose again after exception. But instead my code goes to catch block and loop infinitely and gives me messages :

"--------------------------------------------------------"
"Make your choice :"
"For integer - CHOOSE 1"
"For floating-point numbers - CHOOSE 2"
"You have made a wrong selection. Try again"


"--------------------------------------------------------"
"Make your choice :"
"For integer - CHOOSE 1"
"For floating-point numbers - CHOOSE 2"
"You have made a wrong selection. Try again"


"--------------------------------------------------------"
"Make your choice :"
"For integer - CHOOSE 1"
"For floating-point numbers - CHOOSE 2"
"You have made a wrong selection. Try again"

end so on ...

It don't give me an oportunity to choose again. Can someone explain me please what am I doing wrong ? Thanks

Dear Deer
  • 515
  • 1
  • 11
  • 29
  • if `inChoice` throws exception, it means that value was not consumed from scanner so each time you try to execute `nextInt` it tries to parse same incorrect value. Consume it by adding `next()` in `catch` section. Also avoid try-catch and simply use if-else with `hasNextInt()` condition. – Pshemo May 20 '16 at 12:25
  • Use `intChoice = Integer.parseInt(inChoice.nextLine())`. And make your variable names different so an `int` and a `Scanner` don't differ by 1 character! – Andy Turner May 20 '16 at 12:28
  • @Pshemo how can I retrieve my value from scanner after `hasNextInt()`. I mean it returns boolean value. Say I checked if my scanner has a valid Integer number. How to retrieve my number afterwards ? – Dear Deer Jun 03 '16 at 11:38
  • `hasNextInt()` simply checks if provided value is integer. So if it return `true` simply use `nextInt()` and you should get value as `int`. If it returns `false` then input either is not series of digits, or its value is out of `int` range. In that case you can use `next()` to consume such token and ask user for new one. – Pshemo Jun 03 '16 at 17:15

1 Answers1

1

When the exception occurs, the input is not consumed. It is left there. You need to consume it, otherwise it will keep throwing the exception.

You just need to add this line in the exception block (of-course before continue):

inChoice.next();
dryairship
  • 6,022
  • 4
  • 28
  • 54
  • `inChoice.next();` expects my to hit any key. I would like to "skip" this step with the same functionality. How can I do that ? – Dear Deer Jun 03 '16 at 12:14
  • _"`inChoice.next();` expects my to hit any key_" no. `inChoice.next();` expects you to hit any key and/or press enter. – dryairship Jun 03 '16 at 12:18