简体   繁体   English

Java 输入不匹配异常

[英]Java InputMismatchException

I have this code and I want to catch the letter exception but it keeps having these errors:我有这个代码,我想捕获字母异常,但它一直有这些错误:

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)

And here is my code:这是我的代码:

 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");

     }
 }    

You can use a do-while loop instead to eliminate the first input.nextInt() .您可以改用 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);

Therefore all InputMismatchException can be handled in one place.因此,所有InputMismatchException都可以在一处处理。

from the doc文档

Scanner.nextInt Scans the next token of the input as an int. Scanner.nextInt 将输入的下一个标记扫描为 int。 if the next token does not match the Integer regular expression, or is out of range如果下一个标记与 Integer 正则表达式不匹配,或者超出范围

So it seems you are not entering any integer as input.因此,您似乎没有输入任何整数作为输入。

you can use您可以使用

     while (students <= 0) {

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

            students = input1.nextInt();

         }

         catch (InputMismatchException e) {
             input1.nextLine();
         }
     } 

Reading data from Scanner and assigning it to Int type.从 Scanner 读取数据并将其分配给 Int 类型。 Since you are supplying String this will throw exception.由于您提供 String 这将引发异常。 To handle this situation you must write your snippet inside Try- Catch block only.要处理这种情况,您必须仅在 Try-Catch 块中编写代码片段。

If you wanna be sure about being integer for all input you can try this:如果你想确定所有输入都是整数,你可以试试这个:

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();
            }
        }

John Stef,约翰·斯蒂夫

The method nextInt() just Throws the following exceptions:方法 nextInt() 只是抛出以下异常:

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range NoSuchElementException - if input is exhausted InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围 NoSuchElementException - 如果输入已用完

IllegalStateException - if this scanner is closed IllegalStateException - 如果此扫描器已关闭

If you need/want throw another one type of exception you got to specify your own exception.如果您需要/想要抛出另一种类型的异常,您必须指定自己的异常。 Use throw statement.使用 throw 语句。 Here's an example of a throw statement.下面是一个 throw 语句的例子。

throw someThrowableObject;抛出一些ThrowableObject;

For example:例如:

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());
    }
}

This will catch the both mismatch and negative exceptions.这将捕获不匹配和否定异常。

Although the do... while posted by Siyu Song achieve the desired input from the user, it don't catch negative int exceptions as you wish.尽管由 Siyu Song 发布的 do... 虽然实现了用户所需的输入,但它不会如您所愿捕获负的 int 异常。

You can use this try and do...while from Siyu Song to achieve what you wanting.你可以使用这个 try and do...while 来自 Siyu Song 来实现你想要的。 The complete code looks like this:完整的代码如下所示:

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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM