I am trying to use file scanner in Java to read a txt file and populate a linked list with parse information from the file. The file is tab delimited and needs to get the "name" and "console". The file is formatted similarly to:
10-Yard Fight (5 Screw Cartridge) Nintendo Entertainment System [US]3-D Worldrunner (5 Screw Cartridge) Nintendo Entertainment System [US]720� Nintendo Entertainment System [US]8 Eyes Nintendo Entertainment System [US]Action 52 Nintendo Entertainment System [US]"Addams Family, The" Nintendo Entertainment System [US]Adventure Atari 2600 [NA]"Adventures of Bayou Billy, The" Nintendo Entertainment System [US]"Adventures of Rad Gravity, The" Nintendo Entertainment System [US]Airwolf Nintendo Entertainment System [US]Anticipation Nintendo Entertainment System [US]Armor Ambush (Black Label) Atari 2600 [NA]
Where it does not ever have the next line and immediately exits the while loop. If I move the first four items to the bottom of the txt file like:
Action 52 Nintendo Entertainment System [US]"Addams Family, The" Nintendo Entertainment System [US]Adventure Atari 2600 [NA]"Adventures of Bayou Billy, The" Nintendo Entertainment System [US]"Adventures of Rad Gravity, The" Nintendo Entertainment System [US]Airwolf Nintendo Entertainment System [US]Anticipation Nintendo Entertainment System [US]Armor Ambush (Black Label) Atari 2600 [NA]10-Yard Fight (5 Screw Cartridge) Nintendo Entertainment System [US]3-D Worldrunner (5 Screw Cartridge) Nintendo Entertainment System [US]720� Nintendo Entertainment System [US]8 Eyes Nintendo Entertainment System [US]
it works fine. I am using hasNextLine(). Please tell me if there is something obvious I am overlooking.The code is below:
try{ File file = new File(fileName); Scanner fileScanner = new Scanner(file); int nameCol = 0; int consoleCol = 1; //FILESCANNER DOES NOT HAVE THE NEXT LINE // if(!(fileScanner.hasNextInt()||fileScanner.hasNextLine())) // System.out.println("filescanner does not have next line"); //won't work skips over while loop while(fileScanner.hasNextLine()) { String line = fileScanner.nextLine(); if(line!=""){ String[] items = line.split(DELIM); if(items.length==COLS){ String name = items[nameCol]; String console = items[consoleCol]; VideoGame temp = new VideoGame(name,console); games.add(temp); } } } fileScanner.close(); }catch(FileNotFoundException e){ e.printStackTrace(); System.exit(1); }
I tried using hasNextInt()||hasNextLine() within the while loop. I also know that the system recognizes that the file exists. I apologize for any messy code, as I am new to this. Again, note that the code functions fine with the numerical starting lines at the bottom. Unfortunately, I cannot simply keep the file rewritten and must use the original file as formatted.