.hasNextInt() in switch statement

530 Views Asked by At

I'm basically trying to validate so that you can only enter an Integer. This is what I have at the moment, but if I type letters it goes through the switch and just leaves the result as blank.

I want it so that if anything other than an integer is entered it will go to default in the switch.

Any help would be great. Thanks!

while(loop && kb.hasNextInt())
{   

    choice = kb.nextInt();
    switch(choice)
    {
    case 1 :
            language = "FRENCH";
            loop = false;
            break;
    case 2 : 
            language = "GERMAN";
            loop = false;
            break;
    case 3 :
            language = "SPANISH";
            loop = false;
            break;
    default :
            System.out.println("That is not a correct choice. Please try again!");
            break;

    }
}   
3

There are 3 best solutions below

2
On BEST ANSWER

This is because a letter will cause your while(loop && kb.hasNextInt()) to be false. I suggest put an if statement with the hasNextInt() within the while loop.


Example (using a while loop instead of if statement to really try getting the number):

while(loop)
{
    // validate int using while loop
    while(!kb.hasNextInt())                             
    {
        System.out.println("you must enter a number! ");
        kb.next();
    }

    choice = kb.nextInt();

    switch(choice)
    {
    case 1 :
            language = "FRENCH";
            loop = false;
            break;
    case 2 : 
            language = "GERMAN";
            loop = false;
            break;
    case 3 :
            language = "SPANISH";
            loop = false;
            break;
    }
}   

System.out.println("Thank You " + studentID + " you have been registered for " + language);
0
On

If the next input is not an integer, then .hasNextInt() will return false, and therefore the loop will terminate early.

If you want to allow text input and respond to it, then you need to read line by line, text instead of numbers, and parse the line read with Integer.parseInt. If the line cannot be parsed, you will get a NumberFormatException. You can catch it, and handle appropriately.

    while (loop && scanner.hasNextLine()) {
        String line = scanner.nextLine();
        try {
            choice = Integer.parseInt(line);
        } catch (NumberFormatException e) {
            System.out.println("That is not an integer. Please try again!");
            continue;
        }

        switch (choice) {
            case 1:
                language = "FRENCH";
                loop = false;
                break;
            case 2:
                language = "GERMAN";
                loop = false;
                break;
            case 3:
                language = "SPANISH";
                loop = false;
                break;
            default:
                System.out.println("That is not a correct choice. Please try again!");
                break;
        }
    }
0
On

This code will blow before it even begins if the user did not enter a number as the while required kb.hasNextInt() to be true (have a number) to even run.

What I do is that I usually put the validation around where I receive the input:

int choice;
Boolean retry = null;
while(retry == null) {
    try{
        String input = scanner.nextLine();
        choice = Integer.parseInt(input);
        retry = false;
    }catch(NumberFormatException e){
        System.out.println("Please enter a number from 1 to 4.");
    }
}

switch(choice){
    case 1:
        // Do stuff
        break;
    case 2:
        // Do stuff
        break;
    case 3:
        // Do stuff
        break;
    case 4:
        // Do stuff
        break;
    default:
        System.out.println("Something went wrong!");
}