0
int memberChoice;
    boolean accountLoop = true;
    while (accountLoop) {
        try {
            System.out.println("Would you like to be a... \n1.Basic Member or \n2.Premium member");
            memberChoice = input.nextInt();
            if (memberChoice == 1 || memberChoice == 2) {
                accountLoop = false;
            }
            if (memberChoice < 1 || memberChoice > 2) {
                System.out.println("Please choose either 1 or 2");
            }
            System.out.println();
        } catch (InputMismatchException e) {
            System.out.println("Invalid input");
        }
    }

Whenever I type a string it should say invalid input go back to the beginning of the loop and direct me to input a number again. Instead it gives me an infinite loop. I feel like its something really small but I've been at this for 5 hours and can't figure it out. Any suggestions?

  • add `input.nextLine();`immediatly after `System.out.println("Invalid input");` – Ousmane D. Dec 08 '17 at 23:22
  • When I input a string your code immediately crashes with a `NoSuchElementException`: https://ideone.com/mxqbxC – azurefrog Dec 08 '17 at 23:22
  • 2
    Possible duplicate of [How does input.nextInt() work exactly?](https://stackoverflow.com/questions/28153886/how-does-input-nextint-work-exactly) – Florinache Dec 08 '17 at 23:27

4 Answers4

0

For me, your code workes just fine. You might have a problem with your ide. Anyway change the line if (memberChoice < 1 || memberChoice > 2) to if (memberChoice < 1 && memberChoice > 2)

Michael
  • 120
  • 9
0

Add input = new Scanner(System.in); to the catch block as I assume you are reading from standard input with a Scanner...

This will re-initialise the scanner when something went wrong.

When putting in a "string" the scanner will try to read this as a nextInt() and that just will keep on failing unless you initialise it again...

Ivonet
  • 2,492
  • 2
  • 15
  • 28
0

Using a try/catch block to route your code logic is bad practice. Exceptions should be reserved for exceptional cases, and never used to control the flow of information through an application.

Instead, you can use other methods of the (assumed) Scanner Object to verify the incoming element before trying to assign it a type.

public static void main(String[] args)  {
    int memberChoice=0;
    System.out.println("Would you like to be a... \n1.Basic Member or \n2.Premium member");

    //Use Try with Resources to ensure the Scanner is closed when we're done (requires Java 1.7+)
    try (Scanner input = new Scanner(System.in)){

        //hasNextLine tells us the user has entered a value.
        while (input.hasNextLine() && (memberChoice != 1 && memberChoice != 2)) {

            //Is that value actually an int?
            if (input.hasNextInt()) {
                //YES - Read as int and check for the value we're expecting
                int inValue = input.nextInt();
                if (inValue == 1 || inValue == 2) {
                    memberChoice = inValue;
                } else {
                    System.out.println("Invalid choice.  Try again");
                    System.out.println("Would you like to be a... \n1.Basic Member or \n2.Premium member");
                    System.out.println();
                }
            } else {

                //NO - Read the line, provide feedback and prompt the user for a correction.
                String line = input.nextLine();
                System.out.println("You have to enter an integer value. '"+ line + "' is not valid.");
                System.out.println("Would you like to be a... \n1.Basic Member or \n2.Premium member");
                System.out.println();
            }            
        }
    }
}
Jeremiah
  • 1,145
  • 6
  • 8
0

This is how I solved your problem:

int memberChoice = -1;
boolean accountLoop = true;
Scanner reader = new Scanner(System.in);
while (accountLoop) {
        System.out.println("Would you like to be a... \n1.Basic Member or \n2.Premium member");
        try {   
            memberChoice = reader.nextInt();
            if (memberChoice == 1 || memberChoice == 2) {
                System.out.println(" Answer was valid ");
                accountLoop = false;
            }
            if (memberChoice < 1 || memberChoice > 2) {
                System.out.println("Please choose either 1 or 2");
            }
            System.out.println();
        } catch (InputMismatchException e) {
            System.out.println("Invalid input");
            reader.next(); // this consumes the invalid token 
        }
    }
    reader.close();

This problems was solved using the following link: How to handle infinite loop caused by invalid input using Scanner

Summary of this answer:

You need to "consume" the invalid answer. Otherwise, that input will stay in the buffer which will cause the infinite loop.

Mona Wade
  • 192
  • 1
  • 16