2

I have this problem with a while loop here:

while((input = InputHandler.getInt()) != 1 && input != 2){
        if(input == InputHandler.CODE_ERROR)
            System.out.print("Input must be a number");
    }

This while loop takes in an input only once and doesn't ask for it again, so it loops with that input taken once the entire time. What am I doing wrong here, because to me it's really strange the this wile loop is working?

InputHandler class:

public class InputHandler {
  public static Scanner in = new Scanner(System.in);
  public static int CODE_ERROR = -6;

  public static int getInt(){
    try{
        return in.nextInt();
    } catch(InputMismatchException e){
        return CODE_ERROR;
    }
  }
}
7
  • 9
    Your loop will only terminate when input is simultaneously equal to 1 and 2. That's not going to happen. For any value, either input != 1 or input != 2 is going to be true... I suspect you want &&. Commented May 16, 2014 at 12:48
  • The code appears to have been updated to that now. Commented May 16, 2014 at 12:55
  • @JonSkeet yeah thanks on this one, now it's at least working for correct answers but it still doesn't fix the invalid answer infinite loop state, so to speak. Commented May 16, 2014 at 12:56
  • 2
    I just ran the code as it exists - seems to work correctly? Commented May 16, 2014 at 12:58
  • 1
    I get an endless loop if I supply invalid input. Commented May 16, 2014 at 13:09

2 Answers 2

6

Currently, your code enters an endless loop if a non-integer is entered at the command line. This is because your in.nextInt() method has thrown an exception and has left the offending value in the scanner.

You need to consume the invalid token that caused your exception, by calling in.next();:

public static void main(String[] args) throws Exception {
    int input;
    while ((input = InputHandler.getInt()) != 1 && input != 2) {
        if (input == InputHandler.CODE_ERROR)
            System.out.print("Input must be a number");
    }
}

public static class InputHandler {
    public static Scanner in = new Scanner(System.in);
    public static int CODE_ERROR = -6;

    public static int getInt(){
      try{
          return in.nextInt();
      } catch(InputMismatchException e){
          in.next();  // <------------------ this should solve it
          return CODE_ERROR;
      }
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

I ran the code it is working fine and asking for input each time and when I give 2 or 1 loop terminates. Here is code sample that I ran:-

   package testing;

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

public class Test123 {
    public static Scanner in = new Scanner(System.in);
    public static int CODE_ERROR = -6;

    public static int getInt(){
        try{
            String in1 = in.next();
             return Integer.parseInt(in1);
            //return in.nextInt(); <-- This is leading to error
        } catch(Exception e){
            return CODE_ERROR;
        }
}

    public static void main(String[] args) {
        int input;

        while((input = Test123.getInt()) != 1 && input != 2){
            if(input == Test123.CODE_ERROR)
                System.out.print("Input must be a number");

            System.out.println("\nWrong number: "+input+" Please try again.");


            //input = Test123.getInt();
        }
    }

}

I'm not getting why are you facing problem?

7 Comments

The condition in the while loop will be evaluted every time it loops, so that shouldn't be a problem.
Evan Knowles:- edited post... Sorry for previous dubious comment.
@Vitalij Kornijenko:- Can you please point out if I have ran anything wrong and you want something different. Because to me it is working fine.
@108 The problem is when I input an invalid answer: 3,99,g it goes into an infinite loop instead of taking the input again.
@VitalijKornijenko :- Now I see your problem. See you have explicitly specified in.nextInt(). Which is looking for int. So it gets exception. But standard input stream still has characters. So the loop goes back and try to read same characters fall in exception. Thus leading to infinite loop.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.