3

I keep getting my code caught in an infinite while loop.

It is nothing to advanced, but i can not figure it out for the life of me!

Someone Please help

I have purplosely just re created the specific error without all of the if statements i have in my actual program.

    package bs;

    import java.util.InputMismatchException;
    import java.util.Scanner;

    public class bs {

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);

            boolean continueVar = true;

            while (continueVar) {

                try {
                    System.out.println("Enter Something");
                    int input = sc.nextInt();

                } catch (InputMismatchException i) {
                    System.out.println("What the f***?");
                    continueVar = true;
                }
            }


        }
    }

The infinite loop occurs when the Input mismatch exception is caught. I would think that it would atleast ask the user to re enter their input but instead of doing that it just continues in the loop as so:

    run:
    Enter Something
    df
    What the f***?
    Enter Something
    What the f***?
    Enter Something
    What the f***?

It acts like it is just ignoring the scanner object sc?!

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
Kamron K.
  • 594
  • 1
  • 10
  • 18
  • 6
    You never set continueVar to false in your code so you're always going to have an infinite loop - regardless of whether or not you have an exception. – Anthony Grist Jul 07 '11 at 15:10
  • @Anthony I think he's asking more about why it doesn't reset the scanner object and continuously catches the exception. – Daniel Bingham Jul 07 '11 at 15:14
  • @Daniel I understood that, which is why I commented rather than answering - I couldn't help with the actual problem, but could point out a logic error in the code that would cause problems after he'd fixed the Scanner object issue. – Anthony Grist Jul 07 '11 at 15:25
  • @Anthony My guess is that the logic error is an artifact of his reducing the code down to a small test program. There's probably a lot more going on in the loop and the conditions for setting continueVar to false probably got removed. – Daniel Bingham Jul 07 '11 at 15:29
  • 1
    @Daniel That's likely, but since there's no way to be sure I pointed it out just in case. – Anthony Grist Jul 07 '11 at 15:33
  • Ya, not setting continueVar to false was a left over artifact of making this little postable program rather then pointing out lines of code lol. Thanks a ton!! – Kamron K. Jul 07 '11 at 18:25

4 Answers4

5

No the scanner is not skipped, it's just starting at the beginning of the input. From the JavaDoc:

If the translation is successful, the scanner advances past the input that matched.

This means if the conversion isn't successfull the scanner won't advance. You'd thus have to manually skip the incorrect input using just next().

Edit: you might want to check for hasNextInt() before trying to read the input.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

you loop while continueVar is true, but you never set to to false, so the loop never exits.

I think you want to set continueVar to false in the exception handler.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • Ingore the title, look at his code. The problem is not that he's stuck in an infinite loop -- he wants that -- the problem is that the scanner is not taking new input. – Daniel Bingham Jul 07 '11 at 15:18
  • Exactly! Well in the program an infinite loop is not what i want lol but right here ya ignore the problem that i never set continueVar to false. – Kamron K. Jul 07 '11 at 18:20
0

When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

The token that caused the mismatch is still in the scanner's buffer. You need to clear it before trying to scan again.

You can do that by calling next() in your catch block, like this:

catch (InputMismatchException i) {
    System.out.println("What the f***?");
     sc.next();           
}

Also, you don't need to set continueVar to true again. You never set it to false, so it will stay true. Guessing that's an artifact of you removing this into a mini program.

Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
0

Scanner does not advance when bad token is found. Look at Scanner.java, lines 2095-2096:

catch (NumberFormatException nfe) {
    position = matcher.start(); // don't skip bad token
Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53