NoSuchElementException问题 [英] NoSuchElementException Issue

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

问题描述

我要做的是创建一个while循环,在该循环中运行程序菜单,直到用户输入有效的响应为止。

What I am trying to do is to create a while loop where a program menu is run from until the user inputs a valid response.

但是我的扫描仪实例

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
}

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

And in case it is needed here is my main method:

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.");
  }
}


推荐答案

in2.close();

在第一次成功扫描后,关闭输入流 stdin 已关闭,每次尝试使用 Scanner 从中获取任何信息都会引发 NoSuchElementException

closes the input stream, after the first successful scan, stdin is closed and each attempt to get anything from it with a Scanner causes a NoSuchElementException to be thrown.

这篇关于NoSuchElementException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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