使用Scanner.next()时java.util.NoSuchElementException

时间:2015-06-20 01:48:48

标签: java exception java.util.scanner

Java noob正在处理一个项目,我应该将从文本文件中获取的数据显示在网格上。项目基本上已编写,但输出显示此异常:

run:
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at inputOutput.ReadDataFile.populateData(ReadDataFile.java:50)
    at boggle.Boggle.main(Boggle.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Boggle.java:27链接到我的超类Boggle.java的main方法中的一行代码。该行应该调用我的类ReadDataFile.java中的一个方法。该行读取dataRead.populateData();(// 2.在下面的评论中),在上下文中主要方法如下:

public static void main(String[] args) {            //main() method begins
    // TODO code application logic here
    ReadDataFile dataRead = new ReadDataFile("BoggleData.txt"); //1. instance of class ReadDataFile created

    dataRead.populateData();                        //2. populateData() method called

    boggleData = dataRead.getData();                //3. member variable set equal to getData() method

    Board boggleBoard = new Board(boggleData);      //4. instance of class Board created, passing data as argument

    boggleBoard.shakeDice();                        //5. shakeDice() method called

}                                                   //main() method ends

ReadDataFile.java:50链接到我的子类ReadDataFile.java中名为populateData()的方法中的一行。该行是input.next();,它位于我为该类创建的try-catch-finally的finally组件中。在上下文中,populateData()方法如下所示:

public void populateData(){                 //populateData() method begins          

    try{                                    //try begins

        URL url = getClass().getResource(dataFile); //1. instance of class URL created from file name

        File file = new File(url.toURI());  //2. instance of class File based on url

        input = new Scanner(file);          //3. member variable initialized based on file

        while(input.hasNext()){             //4. while loop goes through data file

            data.add(input.next());         //a. data from file added to ArrayList
        }
    }                                       //try ends

    catch(Exception ex){                    //catch begins
        System.out.println(ex.toString());
        ex.printStackTrace();
    }                                       //catch ends

    finally{                                //finally begins
        input.next();
    }                                       //finally ends
}                                           //populateDate() method ends

基本上,我无法弄清楚如何解决这个异常问题。该项目的实际目标是在网格中显示数据,但我只注意到输出中发现了异常。代码编译得很好,所以我不担心错误的分号或不正确的数据类型。我是Java语言的新手,虽然书籍和其他stackoverflow问题已经解决了我的一些问题,但是这个例外让我陷入困境。

是否有人能够提供一些反馈,说明我需要做些什么来解决我的输出中显示的异常,导致它的原因,或者至少引导我朝着正确的方向前进?我非常感谢任何有用的评论。感谢。

2 个答案:

答案 0 :(得分:3)

您的异常堆栈跟踪显示问题所在:

at inputOutput.ReadDataFile.populateData(ReadDataFile.java:50)

在第50行你有这个:

finally{                                //finally begins
    input.next();
}

问题是你已经用先前执行过的循环耗尽了文件:

while(input.hasNext()){             //4. while loop goes through data file

    data.add(input.next());         //a. data from file added to ArrayList
}

答案 1 :(得分:0)

我认为你的close中的finally意味着你。

finally{                                //finally begins
    input.next();
}      

应该(几乎可以肯定)

finally{                     
    input.close();
}      

或者您可以使用try-with-resources close Scanner类似

public void populateData(String dataFile) {
    try {
        URL url = getClass().getResource(dataFile);
        File file = new File(url.toURI());
        try (Scanner input = new Scanner(file)) {
            while (input.hasNext()) {
                data.add(input.next());
            }
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
        ex.printStackTrace();
    }
}