簡體   English   中英

Java 輸入不匹配異常

[英]Java InputMismatchException

我有這個代碼,我想捕獲字母異常,但它一直有這些錯誤:

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

     }
 }    

您可以改用 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都可以在一處處理。

文檔

Scanner.nextInt 將輸入的下一個標記掃描為 int。 如果下一個標記與 Integer 正則表達式不匹配,或者超出范圍

因此,您似乎沒有輸入任何整數作為輸入。

您可以使用

     while (students <= 0) {

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

            students = input1.nextInt();

         }

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

從 Scanner 讀取數據並將其分配給 Int 類型。 由於您提供 String 這將引發異常。 要處理這種情況,您必須僅在 Try-Catch 塊中編寫代碼片段。

如果你想確定所有輸入都是整數,你可以試試這個:

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

約翰·斯蒂夫

方法 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 來自 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM