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

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

问题描述

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

 输入= kb.nextInt(); 

堆栈跟踪:


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



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



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



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

在java.util.Scanner.nextInt处的

(未知源)

在main.MainDriver.main(MainDriver.java:50处的


相关代码:

  do 
{
displayFullMenu();
System.out.print( Selection:);
输入= kb.nextInt();

开关(输入)
{
//创建新调查
情况1:currentSurvey = new Survey();
休息时间;

//显示当前调查
情况2:currentSurvey.display();
休息时间;

//保存当前调查
情况3:saveSurvey(currentSurvey);
休息时间;

//加载调查
情况4:currentSurvey = loadSurvey();
休息时间;

//修改调查
情况5:currentSurvey.modify();
休息时间;

/ *******************测试功能******************* /

//创建新测试
情况6:currentSurvey = new Test();
休息时间;

//显示当前测试
情况7:currentSurvey.display();
休息时间;

//保存当前测试
情况8:saveSurvey(currentSurvey);
休息时间;

//加载测试
案例9:currentSurvey = loadTest();
休息时间;

//修改测试
情况10:currentSurvey.modify();

默认值:System.out.println(无效的选择。请选择有效的选择:);
输入= kb.nextInt();
System.out.println();
}
} while(输入!= 99);
kb.close();

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



有什么用?

解决方案

< blockquote>

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


是的,可能是问题。如果该 Scanner kb System.in ?) c $ c>并为 close() d,将关闭基础流,并且 kb 无法再获取输入。


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

 input = kb.nextInt();

Stack trace:

Exception in thread "main" java.util.NoSuchElementException

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

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

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

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

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

Relevant code:

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();

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.

What gives?

解决方案

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?

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天全站免登陆