Giall0
Giall0

Reputation: 13

java.util.NoSuchElementException when using Scanner.next()

Java noob working on a project where I'm supposed to display data obtained from a text file onto grids. Project is essentially written, but output displays this exception:

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 links to a line of code in the main method of my superclass Boggle.java. The line is supposed to call one of the methods in my class ReadDataFile.java. The line reads dataRead.populateData(); (//2. on the comments below), and in context the main method looks like:

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 links to a line in a method called populateData() inside of my subclass ReadDataFile.java. The line is input.next(); and it's in the finally component of a try-catch-finally I created for the class. In context, the populateData() method looks like:

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

Basically, I'm having trouble figuring out how I can get around this exception. The actual goal of the project is to display data in grids, but I only get a notice that an exception has been found in the output. The code compiles fine, so I'm not worried about misplaced semicolons or incorrect data types. I'm new to the Java language, and while books and other stackoverflow questions have solved some of my problems, this exception has gotten me stuck.

Would anybody be able to provide some feedback on just what I need to do to get around the exception showing up in my output, what's causing it, or at least steer me in the right direction? I'd really appreciate any helpful comments. Thanks.

Upvotes: 1

Views: 771

Answers (2)

Brent Arias
Brent Arias

Reputation: 30195

Your exception stack-trace shows where the problem is:

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

At line 50 you have this:

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

The problem is that you have already exhausted the file with a loop you previously executed:

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

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

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201507

I think you meant to close in your finally.

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

should (almost certainly) be

finally{                     
    input.close();
}      

Or you could use try-with-resources to close your Scanner like

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

Upvotes: 0

Related Questions