0

In my program I'm trying to get a user to input an int between 1-3 and then do something based off what they type. If it is not a number or not one of the options then it will allow them to reenter a valid option.

The issue I have is I'm having trouble brainstorming how to not have it infinitely loop and just allow them to enter in a number after the console tells them they entered an invalid input.

int i = 0;
while (i < 1) {
    try {
        int level = scan.nextInt();
        i+=1;
        if (level == 1) {
            System.out.println("You selected level 1!");
            //Start the game
        } else if (level == 2) {
            System.out.println("You selected level 2!");
            //Start the game
        } else if (level == 3) {
            System.out.println("You selected level 3!");
            //Start the game
        } else {
            System.out.println("That's not an option!");
            i-=1;
        }
    } catch(InputMismatchException input) {
        System.out.println("That's not an option!");
        i-=1;
    }
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • It is not clear what you want to achieve – Dici Oct 31 '14 at 23:18
  • I need a way to check that the user entered in a valid input between 1-3 but if they dont they have the ability to re-enter a valid input. –  Oct 31 '14 at 23:25
  • See my answer, I think it is better than the current accepted answer since the code is factorized and the exit condition of the loop clearer. Using `while (true)` loops with a break is not a good programming style – Dici Oct 31 '14 at 23:32
  • @Dici Yes I did and I implemented that in place of what I had –  Nov 01 '14 at 00:40
  • Okay, I said that when the accepted answer was not the current one. It is true that I had not seen the problem in the `catch` clause and thus my answer was not complete – Dici Nov 01 '14 at 00:43

4 Answers4

2

When you input an invalid input, you need to clear it. Add scan.next() when input exception triggered so as to clear it with next():

 catch(InputMismatchException input) {
        System.out.println("That's not an option!");
        scan.next();
        i-=1;
    }
P.P
  • 117,907
  • 20
  • 175
  • 238
  • the else is for if they enter an integer that isn't numbers 1-3 –  Oct 31 '14 at 23:36
  • @BlueMoon No, it won't. For example, inputting `4` would not throw any exception (unless you throw it by yourself) even if it is an invalid input – Dici Oct 31 '14 at 23:36
  • @BaileyTincher I see. It makes sense. – P.P Oct 31 '14 at 23:36
0

Not quite the answer you were expecting, but: refactor this code. Remember the basics of java, where every functional bit has its own method. So use a method that reads the input, and returns the level selected (or -1 if nothing):

int readInput() {
  // your code here, returning either the level or -1 on bad input
}

And then call that for your read loop:

int selected;
do {
  selected = readInput();
} while(selected < 1);
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
0

You are better off writing the code like this:

while(true){
    try{
        int level = scan.nextInt();
        if(level==1){
            System.out.println("You selected level 1!");
            break;
        }else if(level==2){
            System.out.println("You selected level 2!");
            break;
        }else if(level==3){
            System.out.println("You selected level 3!");
            break;
        }else{
            System.out.println("That's not an option!");
            continue;
        }
    }catch(InputMismatchException input){
        System.out.println("That's not an option!");
        continue;
    }
}

continue will immediately resume execution of the loop at the top, and break will immediately jump too the closing brace } of the while. This removes the use of the i counter variable, which was entirely useless to the code. Also, this code will never run indefinitely, unless the user indefinitely enters improper values!

Hope this helped, good luck!

phantom
  • 1,457
  • 7
  • 15
0

You can proceed in a much simpler way. The 3 valid cases are very similar and can be treated as one, the game can be started only once after the loop because we know that once the loop exits, level has a valid value.

boolean valid = false;
int level;
do  {
    try {
        level = scan.nextInt();
        valid = 1 <= level && level <= 3;

        if (valid) {
            System.out.println(String.format("You selected level %d !",level));    
        } else {
            System.out.println("That's not an option!");
        }
    } catch(InputMismatchException input) {
        scan.next();
        System.out.println("That's not an option!");
    }
} while (!valid);

// start the game
Dici
  • 25,226
  • 7
  • 41
  • 82