检查应该是int的用户输入是否为字符串

时间:2018-05-14 22:40:40

标签: java

即使接收到错误的用户输入,我仍然试图通过继续使Sentinel程序更加健壮。如果用户输入是一个不同的int,我已经让它工作,但如果它是一个字符串,或者其他任何东西,程序崩溃了。

我目前的代码尝试是:

 } else if (userInt != 1 && userInt != 2 && userInt != 3 && userInt != 4 && userInt != 5 && userInt !=6
            || userInt instanceof String) {

此代码的第一部分可以很好地检查用户输入是否不同.instartof语句给出错误"不兼容的操作数类型int和String"

我是否应该使用instanceof语句?有没有更好的方法来检查这个?

这是整个方法:

public static void printMenu() {

    Scanner userInput2 = new Scanner(System.in);

    String menu = new String("    Please choose from the following menu: \n    1. Rock paper Scissors\n    2. "
            + "Tip Calculator\n    3. "
            + "Number Adding\n    4. Guessing Game\n    5. Random\n    6. Exit");
    System.out.println(menu);
    int userInt = userInput2.nextInt();

    if (userInt == 1) {
        System.out.println("    You asked to play Rock Paper Scissors");
        System.out.println("    Launching Rock Paper Scissors... \n");
        RockPaperScissors gameRun1 = new RockPaperScissors();
        gameRun1.main(null);
    } else if (userInt == 2) {
        System.out.println("    You asked to run the Tip Calculator");
        System.out.println("    Launching the Tip Calculator... \n");
        TipCalculator gameRun2 = new TipCalculator();
        gameRun2.main(null);
    } else if (userInt == 3) {
        System.out.println("    You asked to run the Number Adding game");
        System.out.println("    Launching the Number Adding game... \n");
        NumberAddingGame gameRun3 = new NumberAddingGame();
        gameRun3.main(null);
    } else if (userInt == 4) {
        System.out.println("    You asked to play GuessingGame");
        System.out.println("    Launching GuessingGame... \n");
        GuessingGame gameRun4 = new GuessingGame();
        gameRun4.main(null);
    } else if (userInt == 5) {
            System.out.println("    You asked for a random game");
            option5();
    } else if (userInt == 6) {
            System.out.println(    "Thank you for using Conner's Sentinel");
            // figure out how to terminate the program from here

    } else if (userInt != 1 && userInt != 2 && userInt != 3 && userInt != 4 && userInt != 5 && userInt !=6
            || userInt instanceof String {
        System.out.println("Not a valid input, type 1-6");
        printMenu();
    }
    printMenu();
}

4 个答案:

答案 0 :(得分:2)

无法检查下一个输入 是否与int一样userInput2.nextInt()只能返回{{ 1}}),而是在分配结果之前必须检查。像,

int

答案 1 :(得分:2)

而不是“期待”echo '<meta http-equiv="refresh" content="1;url=http://localhost/bn/login.html">'; ......

int

您应该“期待”int userInt = userInput2.nextInt(); ...

String

然后,您可以使用int actualInput = userInput2.nextLine(); 尝试将Integer.parseInt(String)解析为String,这将为您提供第一次验证值的机会。

问题是int可以抛出Integer.parseInt(String),你真的应该避免根据异常做出逻辑决定。

另一种方法可能是使用正则表达式,例如......

NumberFormatException

如果您对if (actualInput.matches("^\\d*")) { // This is a number, safe to parse to int } else { // This is not a number and is not safe to be parsed } 是一个数字感到满意,则可以使用另一个actualInput来获取下一个Scanner ...

int

作为例子

答案 2 :(得分:1)

userInt instanceof String将始终为false,因为userInt是int。如果是int,则不需要检查instanceof string。

您的意思是使用StringUtils.isNumeric来验证来自用户输入的字符串,并将其他表达式减少为: if 1<= userInt && userInt <=6

  

我是否应该使用instanceof语句?有没有更好的办法   检查一下?

即使某些其他字符串(非数字)可以转换为int并导致1到6之间的值,这样它也会被拒绝。保持数字检查,是的,只是正确的,StringUtils.isNumeric。

如果您选择将userInt改为String,则将另一个表达式转换为if 1<= Integer.parseInt(userInt) && Integer.parseInt(userInt) <=6

答案 3 :(得分:0)

首先,在你写完这样的东西之后:

int userInt = userInput2.nextInt();

您的userInt被声明为int,并且只能是int。所以写这样的东西毫无意义:

userInt instanceof String

因为在这里,您已经知道userIntint,因为您声明了这一点。

问题(发生异常或发生崩溃的地方)在其他地方。它会在调用nextInt()时发生。

阅读Scanner.nextInt()的文档,但在例外情况下,它声明:

  

抛出:   InputMismatchException - 如果下一个标记与Integer正则表达式不匹配,或者超出范围

这正是发生的事情。

你有几个选择,其中两个:

  1. 捕获异常,并按照您希望的方式处理它
  2. 使用Scanner.hasNextInt()
  3. 我已经看到越来越多的替代方法。

    此外,大多数人会将if / else if语句链转换为switch / case / default结构。

    然后是printMenu()方法中的另一个问题,它是无休止的递归。即使它只是用户输入,并且它可能具有有限的时间跨度,在退出之前用户条目有限,理论上您可能会遇到StackOverflowException的情况。这个实现需要从递归转换为迭代,以避免对象的过度分配(内存泄漏)和堆栈永远增长。

相关问题