【问题标题】:Java InputMismatchExceptionJava 输入不匹配异常
【发布时间】:2013-05-24 20:28:03
【问题描述】:

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

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

     }
 }    

【问题讨论】:

  • 第一个 students = input.nextInt(); 不在 try 块内,您输入的内容无法存储在 int 中。
  • 是的,似乎是这种情况,但我怎样才能同时检查两者(字母和负数异常)?
  • 简单。只需删除您发布的前 2 行代码即可。
  • 如果我删除前两行,那么我会在 while 循环中得到一个错误,因为学生不会有值
  • 不,你不会。 students 是 int,它们总是有一个值(默认为 0)。

标签: java exception-handling while-loop try-catch mismatch


【解决方案1】:

您可以改用 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 都可以在一个地方处理。

【讨论】:

  • 这很好。请记住,通过将其设置为 0 来预先初始化您的学生变量。否则您将在编译期间收到“变量可能尚未初始化”错误
  • 如前所述,如果发生 InputMismatchException,它将打印Enter the number of studentsEnter the number of students: 。用System.out.println("Invalid input. Please enter a number.") 之类的东西替换第二个打印可能会很好,这样用户就知道他们做了他们不应该做的事情,所以你在提示和“错误消息”之间有一条线。
【解决方案2】:

来自doc

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

看来您没有输入任何整数作为输入。

你可以使用

     while (students <= 0) {

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

            students = input1.nextInt();

         }

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

【讨论】:

  • 我已将其声明为 int
  • 如果有人键入了abc 之类的字符串,或者他键入了无法用int 表示的数字,nextInt() 方法将抛出异常。见docs.oracle.com/javase/7/docs/api/java/util/…。
  • 我知道唯一正确的输入是整数,但我想捕捉用户按下 0 或负数的情况,我使用 while 循环修复它,如果用户意外按下了字母而不是数字。在那种情况下,我希望得到正确处理并且代码不会中断
  • 你解决了这个问题吗?如果有任何问题,您可以更新问题
  • 如何同时捕捉字母和负数或零数?因为我认为错误在于 while 循环。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      约翰·斯蒂夫,

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

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

      虽然做...虽然宋思雨发帖达到了预期的效果 来自用户的输入,它不会如你所愿捕获负 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多