简体   繁体   English

扫描程序Try Catch语句导致java.util.InputMismatchException

[英]Scanner Try Catch Statement results in java.util.InputMismatchException

Why is my code giving me an error? 为什么我的代码给我一个错误? If the user entered a wrong number shouldn't the code let me enter a new valid number? 如果用户输入了错误的号码,密码不应该让我输入一个新的有效号码吗? Seems as though it doesn't let me change favorite to a new value. 似乎并没有让我将“收藏夹”更改为新值。 How could I get around this problem? 我如何解决这个问题?

package RobB;

import java.util.Scanner;

public class FavoriteNum {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] num = new int[10];
        int favorite = 0;

        System.out.print("Enter your favorite number: ");
        try {
            favorite = scan.nextInt();
        }
        catch (Exception e) {
            System.out.println("Enter an integer!");
            System.out.print("Enter your favorite number: ");

            favorite = scan.nextInt();
        }

        for (int i = 0; i < 10; i++) {
            System.out.print("Enter a random number (" + Math.abs(((i + 1) - 10)) + " to go): ");
            num[i] = scan.nextInt();
        }
    }
}

Console output: 控制台输出:

Enter your favorite number: 11.1

Enter an integer!

Exception in thread "main" Enter your favorite number: java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at RobB.FavoriteNum.main(FavoriteNum.java:21)

This is an alternative with a while loop: 这是一个while循环的替代方法:

boolean validInput = false;
while (!validInput) {
   try {
        System.out.print("Enter your favourite number: ");
        favorite = scan.nextInt();
        validInput = true;
    }
    catch (Exception e) {
        System.out.println("Enter an integer!");
    }
}

Scanning for input in catch clause seems to be not the best idea. 扫描catch子句中的输入似乎不是最好的主意。 I would suggest using a do-while loop and the condition allowing to leave this loop could be a boolean flag which state would be changed when you confirmed that the valid Integer has been finally entered. 我建议使用do-while循环,允许退出此循环的条件可以是boolean标志,当您确认有效的Integer最终输入后​​,状态将被更改。 You might also want to consider using Scanner 's hasNextInt() method to check if the correct input has been provided, without throwing any exception if it is not really necessary in you case. 您可能还想考虑使用ScannerhasNextInt()方法来检查是否提供了正确的输入,如果在您实际不需要的情况下不抛出任何异常。 Here is a little variation of ronhash's answer, but using a do-while loop: 这是ronhash答案的一些变体,但使用了do-while循环:

boolean validInput = false;
do {
    try {
        System.out.print("Enter your favourite number: ");
        favorite = scan.nextInt();
        validInput = true; }
    catch (Exception e) {
        System.out.println("Enter an integer!"); }
} while (!validInput)

Edit: I edited the code because the previous one was inneficient and wrong. 编辑:我编辑了代码,因为前一个代码是无效的和错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 扫描器类抛出 java.util.InputMismatchException - Scanner class throwing java.util.InputMismatchException java.util.InputMismatchException 扫描仪问题? - java.util.InputMismatchException scanner issue? 为什么即使我导入 java.util.InputMisMatchException try/catch InputMisMatchException 也不起作用? - Why try/catch InputMisMatchException doesnt work even if I import java.util.InputMisMatchException? java.util.InputMismatchException - java.util.InputMismatchException 使用Scanner.nextFloat JAVA的java.util.InputMismatchException - java.util.InputMismatchException using scanner.nextFloat JAVA Java扫描仪的next(“。”)给我java.util.InputMismatchException - Java Scanner next(“.”) give me java.util.InputMismatchException 线程“main”java.util.InputMismatchException中的异常:使用scanner - Exception in thread “main” java.util.InputMismatchException: using scanner 使用scanner,java.util.InputMismatchException从文件读取double - reading double from a file using scanner, java.util.InputMismatchException 如何为扫描仪文件阅读器修复java.util.InputMismatchException? - How to fix java.util.InputMismatchException for my scanner file reader? Scanner.next…为Float抛出java.util.InputMismatchException,而不为Int抛出 - Scanner.next… throws java.util.InputMismatchException for Float but not for Int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM