我无法弄清的扫描仪错误:NoSuchElementException [英] Scanner error that I can't figure out: NoSuchElementException

查看:94
本文介绍了我无法弄清的扫描仪错误:NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在do-while循环的第三行崩溃,并且不等待我的输入:

It's crashing on the third line inside the do-while loop, and doesn't wait for my input:

 input = kb.nextInt();

堆栈跟踪:

线程"main"中的异常; java.util.NoSuchElementException

Exception in thread "main" java.util.NoSuchElementException

在java.util.Scanner.throwFor(未知来源)

at java.util.Scanner.throwFor(Unknown Source)

在java.util.Scanner.next(未知来源)

at java.util.Scanner.next(Unknown Source)

在java.util.Scanner.nextInt(未知来源)

at java.util.Scanner.nextInt(Unknown Source)

在java.util.Scanner.nextInt(未知来源)

at java.util.Scanner.nextInt(Unknown Source)

在main.MainDriver.main(MainDriver.java:50)

at main.MainDriver.main(MainDriver.java:50)

相关代码:

do
    {
        displayFullMenu();
        System.out.print("Selection: ");
        input = kb.nextInt();
        
        switch (input)
        {
        //Create new survey
        case 1:     currentSurvey = new Survey();
                    break;
        
        //Display current survey            
        case 2:     currentSurvey.display();
                    break;
        
        //Save current survey           
        case 3:     saveSurvey(currentSurvey);
                    break;
                    
        //Load a survey
        case 4:     currentSurvey = loadSurvey();
                    break;
        
        //Modify a survey
        case 5:     currentSurvey.modify();
                    break;
                    
        /*******************Test Functions*******************/
                    
        //Create new test
        case 6:     currentSurvey = new Test();
                    break;
        
        //Display current test
        case 7:     currentSurvey.display();
                    break;
        
        //Save current test
        case 8:     saveSurvey(currentSurvey);
                    break;
                    
        //Load a test
        case 9:     currentSurvey = loadTest();
                    break;
                    
        //Modify a test
        case 10:    currentSurvey.modify();
                    
        default:    System.out.println("Invalid choice. Please make a valid choice: ");
                    input = kb.nextInt();
                    System.out.println();
        }
    } while (input != 99);
    kb.close();

选择选项9后,它崩溃.它正确保存了文件,然后返回到循环的顶部,并在前面提到的行崩溃.我要它请求更多输入.

It crashes after I choose option 9. It saves the file correctly, then goes back to the top of the loop, and crashes at the previously mentioned line. I want it to ask for more input.

有什么作用?

推荐答案

当我在saveSurvey()中选择选项8时,它必须创建一个新的Scanner(在该方法中),因为所有这些都在我的主方法中.可能是问题所在吗?

When I choose option 8, in saveSurvey(), it has to create a new Scanner (in that method) because all of this is inside my main method. Could that be the issue?

是的,这可能是问题所在.如果Scannerkb具有相同的源(System.in?)并且是close() d,则将关闭基础流,并且kb无法再获取输入.

Yes, that could be the issue. If that Scanner has the same source (System.in?) as kb and is close()d, that closes the underlying stream, and kb cannot get input anymore.

这篇关于我无法弄清的扫描仪错误:NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆