如何克服线程“main”java.util.InputMismatchException中的异常

How to overcome Exception in thread "main" java.util.InputMismatchException

提问人:Prithviraj Tagadinamani 提问时间:6/24/2021 更新时间:6/24/2021 访问量:501

问:

程序:


import java.util.Scanner;

public class JavaExercise1 {
    static int sum, num1, num2;
    static Scanner scan = new Scanner(System.in);
    public static void add(){
        System.out.print("Enter first number to be added: ");
        num1 = scan.nextInt();
        System.out.print("Enter second number to be added: ");
        num2 = scan.nextInt();
        sum=num1+num2;
        System.out.println(num1+" + "+num2+" = "+sum);
    }
    static void sub(){
        System.out.print("Enter first number to be substracted: ");
        num1 = scan.nextInt();
        System.out.print("Enter second number to be substracted: ");
        num2 = scan.nextInt();
        sum=num1-num2;
        System.out.println(num1+" - "+num2+" = "+sum);
    }
    static void mul(){

            System.out.print("Enter first number to be multiplied: ");
            num1 = scan.nextInt();
            System.out.print("Enter second number to be multiplied: ");
            num2 = scan.nextInt();
            sum=num1*num2;
            System.out.println(num1+" x "+num2+" = "+sum);
    }
    static void div(){
        try{
            System.out.print("Enter first number to be divided: ");
            num1 = scan.nextInt();
            System.out.print("Entr second number to be divided: ");
            num2 = scan.nextInt();
            sum=num1/num2;
            System.out.println(num1+" / "+num2+" = "+sum);
        }catch(Exception e){
            System.out.println("\nException occured...");
            System.out.println(e);
        }
    }

    static void printInstructions(){
        System.out.println("1. Addition\n2. Substraction\n3. Multiplication\n4. Division\n5. Exit");
    }
    public static void main(String[] args) {
        int ch;
        do{
            System.out.println("\nEnter your choice: (Choose 0 to print instructions)");
            ch = scan.nextInt();
            switch(ch){
                case 0: printInstructions();
                        break;
                case 1: add();
                        break;
                case 2: sub();
                        break;
                case 3: mul();
                    break;
                case 4: div();
                    break;
                case 5: break;
                default:
                    System.out.println("Invalid choice....");
            }
        }while(ch!=5);

    }
}

当我输入一些更大的数字时,会抛出一个线程异常(线程“main”java.util.InputMismatchException中的异常)

输出:输入您的选择:
(选择 0 打印说明) 4 输入第一个要除数的数字:9999999999

发生异常... java.util.InputMismatchException:对于输入字符串:“9999999999”

输入您的选择:(选择 0 打印说明) 线程“main”java.util.InputMismatchException中的异常:对于输入字符串:“9999999999” 在 java.base/java.util.Scanner.nextInt(Scanner.java:2264) 在 java.base/java.util.Scanner.nextInt(Scanner.java:2212) 在 JavaExercise1.main(JavaExercise1.java:52)

进程完成,退出代码为 1

java 异常 inputmismatchexception 线程异常

评论

0赞 Andreas 6/24/2021
为防止异常,请始终先调用,然后再调用。hasNextInt()nextInt()

答:

0赞 Alan 6/24/2021 #1

您的输入比 Java 中最大的输入大:。9999999999int2147483647

如果要处理如此大的数字,请将 your 更改为 ,并将相应的整数更改为 。scan.nextIntscan.nextLonglong

或者,或者另外,使用块来捕获异常并采取适当的操作(例如向用户发送错误消息以重试)。这可以是正常用户输入检查的一部分。对用户输入非常怀疑,因为例如,有人可能会键入而不是数字。try-catch"Prithviraj is the greatest!"

最后但并非最不重要的一点是,欢迎来到 Stacks。