Now I want to catch the exception if the user enters char such has 'a' 'b' and anything other than int , but when he catches the exception I want him to come back to the line : anser = (input.nextInt())-1;
to recieve new input from the user but somehow he just keep going threw the code. So my question is how do I make him come back to the the input line I mentioned?
thats gets an input from the user and I get the loop keep going and going
I debuged it and I dont understand why when he reaches the raw
anser = (input.nextInt()) - 1; he is not stopping and waiting for an input
hope someone can explain it to me.
Rob Spoor points out it is completely possible to avoid the InputMismatchException like this, or something rather similar:
Correct. The input.next() inside the method is used to consume the non-int. The guard is slightly wrong though; it should also check for input.hasNext:
I think I forgot the hasNext() before in the loop, but that could cause an exception with the input.next() call if the stream is closed.