【问题标题】:NoSuchElement exception when using Scanner使用 Scanner 时出现 NoSuchElement 异常
【发布时间】:2013-12-29 08:12:55
【问题描述】:

我正在尝试使用Scanners 在程序中第二次从控制台获取输入,但是当以另一种方法调用第二个Scanner 时,它会出现 NoSuchElement 异常。

如果我从运行 fileReader() 中删除 startMenu() 它可以工作,但是由于某种原因在运行后它会引发异常。

    public class Garden {
    public static final Garden GARDEN = new Garden();
    //variable declartaions removed
    public static void main(String[] args) {
        if (null != args && 0 < args.length) {
            GARDEN.fileName = args[0].trim();
        }
        if (GARDEN.fileName != null) {
            GARDEN.fileReader(GARDEN.fileName);
        } else {
            GARDEN.fileReader();
        }

        GARDEN.startMenu();
        int mainI = 0;
        while (mainI != 1000000) {
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
            }
            GARDEN.daysWeather();
            GARDEN.anotherDay();
            mainI++;
        }
    }


    protected void fileReader() { // asks for file name for config file
        System.out.println("Enter File Name Please");
        Scanner cmdReader = null;
        String cmdInput = null;
        try {
            cmdReader = new Scanner(System.in);
            cmdInput = cmdReader.nextLine();
        } catch (NoSuchElementException noSuchElement) {
            noSuchElement.printStackTrace();
            fileReader();  //throwing error here
        }

        //code removed
    }



    protected void startMenu() {// modified code from ATM lab (week2)
    int selected = 0;
        //code removed 
        Scanner climateScanner = new Scanner(System.in);
        System.out.println("Select a number 1-4");
        selected = climateScanner.nextInt();
        switch (selected) {
        case 1: // semiarid
            weatherType = 10; //10% chance to rain
            climateScanner.close();
            break;
        case 2: // arid
            weatherType = 20; //5% chance to rain
            climateScanner.close();
            break;
        case 3:
            weatherType = 50; //2% chance to rain
            tropicalWeather = true;
            climateScanner.close(); 
            break;
        case 4:
            weatherType = 20;//5% chance to rain 
            storming = true;
            climateScanner.close();
            break;
        default:
            System.out.println("Invalid Input try again");
            startMenu(); //using Recursion to ask for input again
            break;
        }
        //code removed
    }
}

【问题讨论】:

  • GARDEN 是创建的单身人士不认为这是问题,因为当 GARDEN.startMenu() 未被调用时,它与 GARDEN.fileReader() 一起使用

标签: java java.util.scanner nosuchelementexception system.in


【解决方案1】:

问题是 2 个不同的 Scanner(一个在 startMenu() 中,一个在 readFile() 中)改变了它,所以在类变量中有一个 Scanner scanner = new Scanner(System.in) 然后从内部调用scanner.nextLine()方法

【讨论】:

    【解决方案2】:
     GARDEN.startMenu();// method id not a static one.
    

    您无法以这种方式访问​​它。您必须初始化类或使您的方法静态。还有GARDEN是什么??

    好的,现在你已经编辑了你的代码。

    再次

     GARDEN.fileReader(GARDEN.fileName); // you are parsing input argument 
                                    // But method in your class is no argument method
    

    【讨论】:

    • 或者使startMenu static.
    • fileReader(GARDEN.fileName) 只是 fileReader() 的另一个实例,而不是在控制台中启动程序时要求 fileName 获取它 java Garden testA.txt 将运行 fileReader(fileName) java Garden 将运行 fileReader()
    • 两种 fileReader() 方法的区别在于,一个接受参数的方法从参数中获取文件名,而没有参数的方法通过扫描仪从控制台输入获取文件名(不确定是不是这样)你的意思是我有另一个同名的方法吗)
    • @user1642671 我建议你使用debuger。然后你很容易就会意识到这个问题
    • 我试过它得到 cmdReader = new Scanner(System.in);然后抛出异常每次使用后我与 System.in 有什么关系吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多