关于Scanner类的java.util.NoSuchElementExeption?

问题描述

该程序的基础是要有一个菜单,该菜单会导致需要完成的各种不同“课程”。 toMenu()方法将运行并正确地将值返回给main方法,并且 method3_13()的开始文本指出“请在下面输入一个正整数: ”,但之后会显示错误消息。这是错误消息:

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Chapter3ShortAnswers.method3_13(Chapter3ShortAnswers.java:67)
    at Chapter3ShortAnswers.main(Chapter3ShortAnswers.java:15)

下面是我到目前为止编写的代码...请帮助;〜;

import java.util.Scanner;
public class Chapter3ShortAnswers {
    public static void main(String []args) {
        
        int lesson = toMenu(); //takes user to menu and returns user-inputted value 
        
        switch (lesson) { // switch statement that leads the user to each method's lesson 
            case 0: System.out.println("Thank you for running this program! Have a great day!");
            case 1: method3_13();  
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
        }
        
    }

/**
 * FINSH THIS AT THE END
 * pre: none
 * post: returns number to lesson
 * while: accept user input number
 */
public static int toMenu() {
    Scanner input = new Scanner(system.in);
    int chapterNum; 
    
    System.out.println("Table of Contents ... Please type the number adjacent to the Lesson you would like to visit \n(Press 0 to go back to the menu,press 0 again to end the program): \n");
    System.out.format("%-10s %8s","Lesson","Input Number\n"); // each lesson has a respective number the user 
// this is the number the user inputs
    System.out.format("%-10s %8s","3.13","1\n");
    System.out.format("%-10s %8s","3.14","2\n");
    System.out.format("%-10s %8s","3.15","3\n");
    System.out.format("%-10s %8s","3.16","4\n");
    System.out.format("%-10s %8s","3.18","5\n");
    System.out.format("%-10s %8s","3.19","6\n");
    System.out.format("%-10s %8s","3.20","7\n");
    System.out.format("%-10s %8s","3.21","8\n");
    
    System.out.println("\nType your number here: ");
    chapterNum = input.nextInt();
    
    input.close();
    
    return chapterNum; // returns the user inputed number back to the main menu,which assigns it to variable "lesson"
        
}

/**
 * 
 */
public static void method3_13() {
    int chapterNum = 0;
    int user;
    
    Scanner input = new Scanner(system.in);
    
    System.out.println("Please enter a positive integer below: ");
    user = input.nextInt();
    
    if(user % 1 == 0 && user >= 0) {
        System.out.println("Thank you for entering a number! To go back to the menu and choose another lesson,press 0. ");
    } else {
        while (user % 1 != 0 || user < 0) {
            if(user % 1 != 0) {
                System.out.println("This is not an integer. Please try again with an integer.");
            } else if (user < 0) {
                System.out.println("This is not a positive ineger. Please try again with a positive integer");
            } else if (user % 1 != 0 && user < 0) {
                System.out.println("This is not an integer,nor is it positive. Please try again with a positive integer.");
            }
        }
        
        if (user % 1 == 0) {
            System.out.println("Thank you for entering a number! To go back to the menu and choose another lesson,press 0.");
            chapterNum = input.nextInt();
            
            if(chapterNum == 0) {
                toMenu();
            }
        }
    }
    
    input.close();
}
}

解决方法

@Mert是正确的,Scanner将关闭基础流(如果可关闭),并且由于您在实例化另一个Scanner时正在使用System.in,因此实际上是在传递闭合流,从而传递NotSuchElementException。请注意nextInt()的扫描仪文档:

@如果输入已用尽,则抛出NoSuchElementException。

对于close():

如果此扫描器尚未关闭,则如果其底层可读文件也实现了Closeable接口,则将调用该可读文件的close方法。

因此,只需在应用程序结束时关闭扫描仪即可。

,

问题是您要使用toMenu方法中的input.close()关闭扫描仪,然后尝试再次使用扫描仪。你不能那样做。

尝试一下:

public static int toMenu() {
        Scanner input = new Scanner(System.in);
        int chapterNum;

        System.out.println("Table of Contents ... Please type the number adjacent to the Lesson you would like to visit \n(Press 0 to go back to the menu,press 0 again to end the program): \n");
        System.out.format("%-10s %8s","Lesson","Input Number\n"); // each lesson has a respective number the user
// this is the number the user inputs
        System.out.format("%-10s %8s","3.13","1\n");
        System.out.format("%-10s %8s","3.14","2\n");
        System.out.format("%-10s %8s","3.15","3\n");
        System.out.format("%-10s %8s","3.16","4\n");
        System.out.format("%-10s %8s","3.18","5\n");
        System.out.format("%-10s %8s","3.19","6\n");
        System.out.format("%-10s %8s","3.20","7\n");
        System.out.format("%-10s %8s","3.21","8\n");

        System.out.println("\nType your number here: ");
        chapterNum = input.nextInt();

        return chapterNum; // returns the user inputed number back to the main menu,which assigns it to variable "lesson"

    }

请确保在使用完扫描仪后始终将其关闭。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...