简体   繁体   中英

Problem with buffered File Reader

I have been asked to write a file reader method, I have done one which works fine but cant get the 2nd one to work and keep getting this error after i open the booms.txt file

Error:java.util.NoSuchElementException

public instance variable

public List booms;

Code I'm using for the file reader

try

  { int x; int y; double boomTime; boolean isAHit; Scanner lineScanner; bufferedFileReader = new BufferedReader(new FileReader(aFile)); String currentLine = bufferedFileReader.readLine(); while (currentLine != null) { lineScanner = new Scanner(currentLine); lineScanner.useDelimiter(","); x = lineScanner.nextInt(); y = lineScanner.nextInt(); boomTime = lineScanner.nextDouble(); isAHit = lineScanner.nextBoolean(); booms.add(new Boom(x,y,boomTime)); currentLine = bufferedFileReader.readLine(); } } catch (Exception anException) { System.out.println("Error:"+anException); } finally { try { bufferedFileReader.close(); } catch (Exception anException) { System.out.println("Error:" +anException); } } } 

Please Help

文件末尾是否有空白行?

I'm using a another method that's not that complicated. just do this:

Scanner diskScanner = new Scanner(new File("pathname"));

to read for example a Integer from the specified file just type

int blahblahblah = diskScanner.nextInt();

as maurice says, probably a blank or incorrect line of data in the file.

the exception (although you haven't posted the stack trace) is being thrown by the Scanner.next... calls.

Scanner.nextInt

NoSuchElementException - if input is exhausted

this would be far more obvious to you and the members of this forum if you'd included the stack trace. try putting anException.printStackTrace(); in your catch blocks next time.

得到排序后,我有一个布尔变量,该变量不在正在读取的文件中:),这一切都与休息并再次查看它有关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM