java.util.NoSuchElementException 关于 Scanner 类?

huangapple go评论77阅读模式
英文:

java.util.NoSuchElementExeption regarding the Scanner class?

问题

这是您提供的代码的翻译部分:

import java.util.Scanner;
public class Chapter3ShortAnswers {
    public static void main(String []args) {
        
        int lesson = toMenu(); //将用户带到菜单并返回用户输入的值
        
        switch (lesson) { // 开关语句将用户引导到每个方法的课程
            case 0: System.out.println("感谢您运行此程序!祝您有美好的一天!");
            case 1: method3_13();  
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
        }
        
    }

    /**
     * 在结尾处完成此部分
     * pre: 无
     * post: 返回到课程的数字
     * while: 接受用户输入的数字
     */
    public static int toMenu() {
        Scanner input = new Scanner(System.in);
        int chapterNum; 
        
        System.out.println("目录... 请键入所需访问的课程旁边的数字\n(按0返回菜单,再次按0结束程序):");
        System.out.format("%-10s %8s", "课程", "输入数字\n"); // 每个课程都有一个相应的数字
        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("\n在此处输入您的数字:");
        chapterNum = input.nextInt();
        
        input.close();
        
        return chapterNum; // 将用户输入的数字返回到主菜单,然后将其分配给变量“lesson”
            
    }

    /**
     * 
     */
    public static void method3_13() {
        int chapterNum = 0;
        int user;
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("请在下方输入一个正整数:");
        user = input.nextInt();
        
        if(user % 1 == 0 && user >= 0) {
            System.out.println("感谢您输入一个数字!要返回菜单并选择另一个课程,请按0。");
        } else {
            while (user % 1 != 0 || user < 0) {
                if(user % 1 != 0) {
                    System.out.println("这不是一个整数。请使用整数再试一次。");
                } else if (user < 0) {
                    System.out.println("这不是一个正整数。请使用正整数再试一次。");
                } else if (user % 1 != 0 && user < 0) {
                    System.out.println("这不是一个整数,也不是正整数。请使用正整数再试一次。");
                }
            }
            
            if (user % 1 == 0) {
                System.out.println("感谢您输入一个数字!要返回菜单并选择另一个课程,请按0。");
                chapterNum = input.nextInt();
                
                if(chapterNum == 0) {
                    toMenu();
                }
            }
        }
        
        input.close();
    }
}
英文:

The basis of this program is to have a menu that leads to various different "lessons" that need to be completed. The toMenu() method runs and returns a value back to the main method correctly, and the beginning text of the method3_13() that states "Please enter a positive integer below: ", but after that an error message is displayed. This is the error message:

Exception in thread &quot;main&quot; 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)

Below is the code that I have written so far... Please help ;~;

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&#39;s lesson 
case 0: System.out.println(&quot;Thank you for running this program! Have a great day!&quot;);
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(&quot;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&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;Lesson&quot;, &quot;Input Number\n&quot;); // each lesson has a respective number the user 
// this is the number the user inputs
System.out.format(&quot;%-10s %8s&quot;, &quot;3.13&quot;, &quot;1\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.14&quot;, &quot;2\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.15&quot;, &quot;3\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.16&quot;, &quot;4\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.18&quot;, &quot;5\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.19&quot;, &quot;6\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.20&quot;, &quot;7\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.21&quot;, &quot;8\n&quot;);
System.out.println(&quot;\nType your number here: &quot;);
chapterNum = input.nextInt();
input.close();
return chapterNum; // returns the user inputed number back to the main menu, which assigns it to variable &quot;lesson&quot;
}
/**
* 
*/
public static void method3_13() {
int chapterNum = 0;
int user;
Scanner input = new Scanner(System.in);
System.out.println(&quot;Please enter a positive integer below: &quot;);
user = input.nextInt();
if(user % 1 == 0 &amp;&amp; user &gt;= 0) {
System.out.println(&quot;Thank you for entering a number! To go back to the menu and choose another lesson, press 0. &quot;);
} else {
while (user % 1 != 0 || user &lt; 0) {
if(user % 1 != 0) {
System.out.println(&quot;This is not an integer. Please try again with an integer.&quot;);
} else if (user &lt; 0) {
System.out.println(&quot;This is not a positive ineger. Please try again with a positive integer&quot;);
} else if (user % 1 != 0 &amp;&amp; user &lt; 0) {
System.out.println(&quot;This is not an integer, nor is it positive. Please try again with a positive integer.&quot;);
}
}
if (user % 1 == 0) {
System.out.println(&quot;Thank you for entering a number! To go back to the menu and choose another lesson, press 0.&quot;);
chapterNum = input.nextInt();
if(chapterNum == 0) {
toMenu();
}
}
}
input.close();
}
}

答案1

得分: 1

@Mert 说得有些对,如果Scanner是可关闭的,则会关闭底层流,由于您在实例化另一个Scanner时正在使用System.in,因此您实际上正在传递一个已关闭的流,从而导致NoSuchElementException。请注意nextInt()的Scanner文档:

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

至于close():

> 如果此Scanner尚未关闭,那么如果其底层可读流还实现了Closeable接口,则会调用可读流的close方法。

因此,在您的应用程序末尾关闭您的Scanner。

英文:

@Mert is sort of right, Scanner will close the underlying stream if it is Closable, and since you are using System.in when you instantiate another Scanner you are effectively passing a closed stream and thus the NotSuchElementException. Note the Scanner documentation for nextInt():

> @throws NoSuchElementException if input is exhausted.

And for close():

> If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked.

So just close your Scanner at the end of your application.

答案2

得分: -1

问题是你在方法toMenu中使用input.close()关闭了Scanner,然后试图立即再次使用scanner。你不能这样做。

尝试这样做:

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(); // 确保你在使用完后始终关闭Scanner

    return chapterNum; // 返回用户输入的数字回主菜单,将其赋给变量"lesson"
}

确保你在使用完Scanner后始终关闭它。

英文:

The problem is you are closing the Scanner with input.close() in method toMenu, and you are trying to use scanner again right after. You can't do that.

Try this:

public static int toMenu() {
Scanner input = new Scanner(System.in);
int chapterNum;
System.out.println(&quot;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&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;Lesson&quot;, &quot;Input Number\n&quot;); // each lesson has a respective number the user
// this is the number the user inputs
System.out.format(&quot;%-10s %8s&quot;, &quot;3.13&quot;, &quot;1\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.14&quot;, &quot;2\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.15&quot;, &quot;3\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.16&quot;, &quot;4\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.18&quot;, &quot;5\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.19&quot;, &quot;6\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.20&quot;, &quot;7\n&quot;);
System.out.format(&quot;%-10s %8s&quot;, &quot;3.21&quot;, &quot;8\n&quot;);
System.out.println(&quot;\nType your number here: &quot;);
chapterNum = input.nextInt();
return chapterNum; // returns the user inputed number back to the main menu, which assigns it to variable &quot;lesson&quot;
}

Make sure that you always close the Scanner after you are done using it.

huangapple
  • 本文由 发表于 2020年9月11日 05:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63837975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定