【问题标题】:Cannot find reason for InputMismatchException找不到 InputMismatchException 的原因
【发布时间】:2017-02-17 07:40:19
【问题描述】:

我在使用 Scanner 时遇到问题,因为它似乎采用输入值类型并在用户下次输入值时强制其为相同类型。我找不到任何原因导致此代码无法正常工作并给我一个 InputMismatchException,因为我已经编写了这样的代码一百万次并且没有遇到问题。

 public void register(){
    Scanner input=new Scanner(System.in);
        System.out.println("What course would you like to register for?");
        String course_name = input.next();
        System.out.println("What section?");
        int section = input.nextInt();

        for (int i = 0; i < courses.size(); i++) {
            if (courses.get(i).getCourse_name().equals(course_name)) {
                if (courses.get(i).getCourse_section() == section) {
                    courses.get(i).AddStudent(this.first_name+" "+this.last_name);
                }
            }
        }
        input.close();
    }

这个问题不仅仅是 register() 方法,而是整个程序,例如使用以下代码:

public void Options() {
    Scanner input=new Scanner(System.in);
    while (true) {
        System.out.println("What would you like to do (Enter corresponding number):" + "\n" + "1) View all courses" + "\n" + "2) View all courses that are not full" + "\n" + "3) Register on a course" + "\n" + "4) Withdraw from a course" + "\n" + "5) View all courses that the current student is being registered in" + "\n" + "6) Exit");
        int user = input.nextInt();
        if (user == 1)
            viewAll();
        if (user == 2)
            viewAllOpen();
        if (user == 3)
            register();
        if (user == 4)
            withdraw();
        if (user == 5)
            viewRegistered();
        if (user == 6) {
            Serialize();
            break;
        }
    }

如果注册方法之一需要用户输入字符串,则 int user=input.nextInt();将导致 InputMismatchException。

【问题讨论】:

标签: java java.util.scanner inputmismatchexception


【解决方案1】:

我已经复制了这段代码,但没有遇到同样的问题。如果用户在提示输入课程编号时输入诸如 11 之类的整数,则代码将正常运行。当然,如果您输入的不是整数,它会抛出 InputMismatchException。请参阅 Scanner#nextInt() 的 Java 文档描述,特别是:

将输入的下一个标记扫描为 int。

以 nextInt() 形式调用此方法的行为与调用 nextInt(radix) 完全相同,其中 radix 是此扫描仪的默认基数。

投掷:

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

Read More

如果您想阻止这种情况并且不想处理 try-catch,您可以暂停执行,直到给出有效整数。

public static void register(){
    Scanner input=new Scanner(System.in);
    System.out.println("What course would you like to register for?");
    String course_name = input.next();
    System.out.println("What section?");
    //Loop until the next value is a valid integer.
    while(!input.hasNextInt()){
        input.next();
        System.out.println("Invalid class number! Please enter an Integer.");
    }
    int section = input.nextInt();
    input.close();
        
    System.out.println(course_name + " " + section);
}

【讨论】:

  • 不是不输入整数的问题。当我运行它时,我什至没有机会输入一个整数,因为 InputMismatchException 在我输入我想注册的课程名称后立即发生并且程序停止。
  • 在扫描到您请求的字符串后立即尝试在 Options 方法中关闭扫描仪。资源仍然打开的事实干扰了在注册方法中打开的扫描仪。如果这不起作用,请粘贴您的堆栈跟踪。 Scanner input=new Scanner(System.in); while (true) { System.out.println("blah"); int user = input.nextInt(); input.close() //Other code here.
猜你喜欢
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 2022-11-24
  • 2019-12-28
  • 2018-08-04
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多