首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoSuchElementException问题

NoSuchElementException问题
EN

Stack Overflow用户
提问于 2012-10-01 06:14:11
回答 1查看 198关注 0票数 2

我要做的是创建一个while循环,程序菜单从这个循环开始运行,直到用户输入一个有效的响应。

然而,我创建的扫描器实例不想玩球,不管我怎么尝试,它都会抛出NoSuchElementExceptions。

代码语言:javascript
复制
import java.util.Scanner;

public class Menu
{
    Menu()
    {
        isValid = false;
        uInput = 0;
    }

    public int mMain()
    {
        Scanner in2;

        do
        {           
            in2 = new Scanner(System.in); //For user input

            System.out.println("Please choose from the following options");
            System.out.println("1) Enter Employee Data");
            System.out.println("2) Look Up Employee Information");
            System.out.println("3) Employee Comparison");
            System.out.println("4) Quit");
            uInput = in2.nextInt(); //This is the line throwing the exception.

            if (uInput < 1 || uInput > 4)
            {
                System.out.println("Your choice was not valid, please choose another option.");
            }

            else
            {
                isValid = true;
                in2.close();
            }
        } while (!isValid);

       isValid = false; //reset for next time
       System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); //Clear screen

       return uInput;       
    }

    int uInput; //For user input
    String uInput2; //for bug fixing
    boolean isValid; //check for valid use input
}

如果需要的话,这里是我的主要方法:

代码语言:javascript
复制
public class prg420_w4_commission_program
{
    public static void main(String[] args)
    {       
    //class references
    Processing proc = new Processing();
    Menu menu = new Menu();

    //Set up Variables
    int uInput; //For user input
    boolean uQuit = false; //set up program loop bool

    //Opening message
    System.out.println("Welcome to the Employee Data Entry Creation program.  Please follow the on-screen prompts.");
    System.out.println("");

    proc.initEDatabase(); //initialize employee database


    while (!uQuit)
    {

        uInput = menu.mMain(); //call main menu

        //Check user input
        switch (uInput)
        {
            case 1:
                proc.addEDBEntry(); //add employee entries
                break;

            case 2:
                proc.findEmployee();
                break;

            case 3:
                proc.sortEDBase();
                break;

            case 4:
                uQuit = true;
                break;

            default:
                proc.addEDBEntry();
                break;                      
        }           
    }

    System.out.println("Thank you for using the Employee Data Entry Creation program.");
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-01 06:24:04

代码语言:javascript
复制
in2.close();

关闭输入流,在第一次成功扫描后,stdin将关闭,并且每次尝试使用Scanner从输入流获取任何内容时,都会引发NoSuchElementException异常。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12665591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档