switch语句中的.hasNextInt()

时间:2014-12-15 20:16:43

标签: java int switch-statement validating

我基本上都在尝试验证,以便您只能输入整数。这就是我目前所拥有的,但是如果我输入字母,它会通过switch并将结果留空。

我想要它,如果输入除整数以外的任何内容,它将转到开关中的default

任何帮助都会很棒。谢谢!

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 个答案:

答案 0 :(得分:1)

如果下一个输入不是整数, 然后.hasNextInt()将返回false, 因此循环将提前终止。

如果您想允许文本输入并对其进行响应, 那么你需要逐行阅读,文字而不是数字, 并解析用Integer.parseInt读取的行。 如果无法解析该行,您将获得NumberFormatException。 你可以抓住它,并妥善处理。

    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;
        }
    }

答案 1 :(得分:0)

这是因为一封信会导致while(loop && kb.hasNextInt())成为false。我建议在if循环中使用hasNextInt()添加while语句。


示例(使用while循环代替if语句来真正尝试获取数字):

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);

答案 2 :(得分:0)

如果用户没有输入数字,则该代码将在它开始之前开始,因为需要kb.hasNextInt()为真(有一个数字)才能运行。

我所做的是我通常将验证放在收到输入的地方:

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!");
}