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


java noob working on project i'm supposed display data obtained text file onto grids. project written, output displays exception:

run: exception in thread "main" java.util.nosuchelementexception     @ java.util.scanner.throwfor(scanner.java:862)     @ java.util.scanner.next(scanner.java:1371)     @ inputoutput.readdatafile.populatedata(readdatafile.java:50)     @ boggle.boggle.main(boggle.java:27) java result: 1 build successful (total time: 0 seconds) 

boggle.java:27 links line of code in main method of superclass boggle.java. line supposed call 1 of methods in class readdatafile.java. line reads dataread.populatedata(); (//2. on comments below), , in context 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 getdata() method      board boggleboard = new board(boggledata);      //4. instance of class board created, passing data argument      boggleboard.shakedice();                        //5. shakedice() method called  }                                                   //main() method ends 

readdatafile.java:50 links line in method called populatedata() inside of subclass readdatafile.java. line input.next(); , it's in component of try-catch-finally created class. in context, populatedata() method looks like:

public void populatedata(){                 //populatedata() method begins                try{                                    //try begins          url url = getclass().getresource(datafile); //1. instance of class url created 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 file added 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 can around exception. actual goal of project display data in grids, notice exception has been found in output. code compiles fine, i'm not worried misplaced semicolons or incorrect data types. i'm new java language, , while books , other stackoverflow questions have solved of problems, exception has gotten me stuck.

would able provide feedback on need around exception showing in output, what's causing it, or @ least steer me in right direction? i'd appreciate helpful comments. thanks.

i think meant close in finally.

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

should (almost certainly) be

finally{                          input.close(); }       

or use try-with-resources close 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();     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -