7

我有这段代码,我想捕捉字母异常,但它一直有这些错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at exercise_one.Exercise.main(Exercise.java:17)

这是我的代码:

 System.out.print("Enter the number of students: ");

 students = input.nextInt(); 

 while (students <= 0) {

     try {

        System.out.print("Enter the number of students: ");

        students = input.nextInt();

     }

     catch (InputMismatchException e) {

        System.out.print("Enter the number of students");

     }
 }    
4

5 回答 5

10

您可以改用 do-while 循环来消除第一个input.nextInt().

do {
    try {
        System.out.print("Enter the number of students: ");
        students = input.nextInt();
    } catch (InputMismatchException e) {
        System.out.print("Invalid number of students. ");
    }
    input.nextLine(); // clears the buffer
} while (students <= 0);

因此,一切InputMismatchException都可以在一个地方处理。

于 2013-05-29T14:27:48.563 回答
3

文档

Scanner.nextInt 将输入的下一个标记扫描为 int。如果下一个标记与整数正则表达式不匹配,或者超出范围

因此,您似乎没有输入任何整数作为输入。

您可以使用

     while (students <= 0) {

         try {
            System.out.print("Enter the number of students: ");

            students = input1.nextInt();

         }

         catch (InputMismatchException e) {
             input1.nextLine();
         }
     } 
于 2013-05-29T14:14:30.977 回答
0

从 Scanner 读取数据并将其分配给 Int 类型。由于您提供 String 这将引发异常。要处理这种情况,您必须仅在 Try-Catch 块中编写代码段。

于 2020-04-12T14:37:51.907 回答
0

如果你想确保所有输入都是整数,你可以试试这个:

while(true) {
            try {
                System.out.print("Kolon sayısını giriniz: ");
                c = scan.nextInt();
                
            } catch (Exception e) {
                System.out.print("Geçersiz giriş.. ");
                scan.nextLine();
                continue;
            }
            break;
        }


// or this...
while(true) {
            System.out.print("Give me a number");
            try {
                input = scan.nextInt();
                break;
            } catch (Exception e) {
                System.out.print("There was mismatch");
                scan.nextLine();
            }
        }
于 2021-06-19T08:23:52.880 回答
0

约翰·斯特夫,

nextInt() 方法只是抛出以下异常:

InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围 NoSuchElementException - 如果输入已用尽

IllegalStateException - 如果此扫描仪已关闭

如果您需要/想要抛出另一种类型的异常,您必须指定自己的异常。使用 throw 语句。这是一个 throw 语句的示例。

抛出一些ThrowableObject;

例如:

try {
    System.out.print("Enter the number of students: ");
    students = input.nextInt();
    if(students <=0){
        throw new Exception("Null or Negative number of students is invalid.");
    }
    } catch (InputMismatchException e) {
        System.out.print("Invalid input. Please enter a number for student number.");
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }
}

这将捕获不匹配和负异常。

尽管 Siyu Song 发布的 do... 虽然实现了用户所需的输入,但它不会如您所愿捕获负 int 异常。

你可以用这个try and do...while from Siyu Song来实现你想要的。完整的代码如下所示:

do {
    try {
           System.out.print("Enter the number of students: ");
           students = input.nextInt();
           if(students <=0){
                throw new Exception("Negative number of students is invalid.");
           }
       } catch (InputMismatchException e) {
             System.out.print("Invalid input. Please enter a number for students number.");
       } catch (Exception e) {
              System.out.print(e.getMessage());
     }
     input.nextLine();
} while (students <=0);
于 2021-09-12T19:02:48.333 回答