简体   繁体   English

扫描仪-控制台菜单应用程序上的NoSuchElementException

[英]Scanner - NoSuchElementException on console menu application

I'm making some tests with Java to work out a menu-driven console application. 我正在用Java做一些测试,以开发出一个菜单驱动的控制台应用程序。

I have this code: 我有以下代码:

    public class TestClass {

    public static void main(String[] args) {

        int option = 99;

        do{
            System.out.println("1. one");
            System.out.println("2. two");
            System.out.println("3. three");
            System.out.println("0. END");

            Scanner scan = new Scanner(System.in);

            System.out.println("option : ");

            option=Integer.parseInt(scan.next());
            scan.close();
            switch (option) {
                case 0 :
                    System.exit(0);
                    break;
                case 1 :
                    caseOne();
                    break;
                case 2 :
                    caseTwo();
                    break;
                case 3 :
                    caseThree();
                    break;
                default :
                    break;

            }


        } while (option!=0);


    }

    private static void caseOne(){
        System.out.println("Case ONE");
    }

    private static void caseTwo(){
        System.out.println("Case TWO");
    }

    private static void caseThree(){
        System.out.println("Case THREE");
    }
}

When I run it it works pretty well. 当我运行它时,效果很好。 I choose one option and it prints out "CASE X". 我选择一个选项,它会打印出“ CASE X”。

The strange behaviour is when the DO loop starts over. 奇怪的行为是DO循环重新开始时。 It shows once again the menu but program immediatly terminates showing a NoSuchElementException exception. 它再次显示菜单,但程序立即终止,并显示NoSuchElementException异常。 The strange thing is that it neither waits for me to enter a digit. 奇怪的是,它既不等待我输入数字。

If a remove the 如果删除

scan.close();

statement it do works fine altough compiler warns me of memory leak since scan is never closed. 声明它确实可以正常工作,但是编译器会警告我内存泄漏,因为从不关闭扫描。

It behaves very strange because on the "first pass" everything is ok even with te scan.close() but on the "second pass" it crashes even if I'm creating a new Scanner object so I think it shoul at least ask for the input. 它的行为非常奇怪,因为即使在te scan.close()的情况下,“第一遍”一切正常,但即使创建新的Scanner对象,“第二遍”也会崩溃,因此我认为它至少应输入。

I really get confused with this. 我真的对此感到困惑。

Any suggestions? 有什么建议么?

if you are closing scanner it will close wrapped input stream also so you can close scan after finishing all of your work. 如果您要关闭scanner ,它将同时关闭包装的input stream因此您可以在完成所有工作后关闭扫描。

scan.close();

put it after do-while loop. 将其放在do-while循环之后。

here the better way you can achieve it 这是实现它的更好方法

public static void main(String[] args) {

    int option = 99;

    do{
        System.out.println("1. one");
        System.out.println("2. two");
        System.out.println("3. three");
        System.out.println("0. END");

        Scanner scan = new Scanner(System.in);

        System.out.println("option : ");

        option= scan.nextInt();
        switch (option) {
            case 0 :
                System.exit(0);
                break;
            case 1 :
                caseOne();
                break;
            case 2 :
                caseTwo();
                break;
            case 3 :
                caseThree();
                break;
            default :
                scan.close();
                break;

        }


    } while (option!=0);


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM