【问题标题】:Scanner returning an error for no reason?扫描仪无缘无故返回错误?
【发布时间】:2016-01-13 01:29:09
【问题描述】:

我正在尝试执行这行代码。我希望它接受一个字符串,如果它是 1,2,3,4,5,6,7,8,9 或 10 将其解析为 int 然后检查它是否在 0 和 9 之间(在 - 1)。问题出在扫描仪发生后,无论我做什么都会出错...

public void guessEnter() {
    Scanner scan = new Scanner(System.in);

    String guessXS;
    String guessYS;
    boolean pass1;
    boolean pass2;

    do{
        do{
            System.out.println("Enter a value for the column!");
            guessXS = scan.nextLine();
            switch(guessXS){
                case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9":case "10":
                    System.out.println("TRUE");
                    pass1 = true;
                    break;
                default:
                    System.out.println("FALSE");
                    pass1 = false;
                    break;
            }
        }while(pass1 == false);
        do{
            System.out.println("Enter a value for the row!");
            guessYS = scan.nextLine();
            switch(guessYS){
                case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": case "10":
                    pass2 = true;
                    System.out.println("TRUE");
                    break;
                default:
                    pass2 = false;
                    System.out.println("FALSE");
                    break;
            }
        }while(pass2 == false);
        guessX = Integer.parseInt(guessXS) - 1;
        guessY = Integer.parseInt(guessYS) - 1;
    }while(guessX >= 0 && guessX <= 9 && guessY >= 0 && guessY <= 9);
}

为什么会发生这种情况我无法理解。我是 java 新手,需要解决这个错误。

错误是... 线程“主”java.util.InputMismatchException 中的异常

at java.util.Scanner.throwFor(Scanner.java:864)

at java.util.Scanner.next(Scanner.java:1485)

at java.util.Scanner.nextInt(Scanner.java:2117)

at java.util.Scanner.nextInt(Scanner.java:2076)

at shipLocation.xyInput(shipLocation.java:11)

at battleShipMain.main(battleShipMain.java:53)

感谢您的帮助。

【问题讨论】:

  • 崩溃时输入的字符串是什么?
  • 为什么不直接使用带有 try-catch 块的 Integer.parseInt() 并然后检查数字范围?
  • 为什么不直接使用带有 try-catch 块的 Integer.parseInt() 然后检查数字范围?因为如果它不作为 int 输入,我想让它成为安全的。 ---- 崩溃时输入的字符串是什么?我尝试了所有这些,除了整数之外,它们都崩溃了。
  • 您是否更改了方法的名称?根据您的堆栈跟踪,错误似乎在名为 xyInput() 的方法中,而不是在 guessEnter() 中。
  • 是的!杰出的!那就是我看错了选择列号println!谢谢。

标签: java string exception input java.util.scanner


【解决方案1】:

即使问题已经得到解答(错误在不同的类中)并且提供的代码没有导致错误,您也可能希望稍微改进一下代码。为了便于阅读和理解,以备日后需要更改时使用。

例如,您可以将输入和验证部分包装在一个方法中:

private int readIntBetweenMinAndMax(String msg, int min, int max) {
    Scanner scanner = new Scanner(System.in);
    System.out.println(msg);
    int userInput;
    do {
        try {
            userInput = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.err.println("Please enter a valid integer");
            continue;
        }
        if (userInput > max || userInput < min) {
            System.err.println("Please enter a integer between " + min + " and " + max);
            continue;
        }
        return userInput;
    } while (true);
}

上面的方法打印一个提供的字符串msg,然后读入一个整数。它检查用户输入是否为整数,以及它是否大于或等于 大于min小于或等于 小于max。如果三个条件之一失败,该方法将打印一条错误消息并提示用户输入新输入,直到其正确为止。
注意:您当然也可以包括“TRUE”和“FALSE”输出如果你愿意

因此,您只需在 guessEnter 方法中调用该方法两次:

public void guessEnter() {
    guessX = readIntBetweenMinAndMax("Enter a value for the column!", 1, 10) - 1;
    guessY = readIntBetweenMinAndMax("Enter a value for the row!", 1, 10) - 1;
}

这将接受一、十和它们之间的任何数字。并像在代码中那样减去一个。

如前所述,这只是一个建议,我不确定你的代码是否正确,但我希望它有所帮助(:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2015-10-02
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多