簡體   English   中英

緩沖文件閱讀器出現問題

[英]Problem with buffered File Reader

我被要求編寫一種文件讀取器方法,我已經完成了一種工作正常的方法,但是在打開booms.txt文件后卻無法使第二種方法正常工作並繼續出現此錯誤。

錯誤:java.util.NoSuchElementException

公共實例變量

公眾名單熱潮;

我用於文件閱讀器的代碼

嘗試

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

請幫忙

文件末尾是否有空白行?

我正在使用另一種方法,它並不那么復雜。 這樣做:

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

從指定文件中讀取例如一個整數

int blahblahblah = diskScanner.nextInt();

如莫里斯所說,文件中的數據行可能空白或不正確。

Scanner.next...調用引發了異常(盡管您尚未發布堆棧跟蹤)。

Scanner.nextInt

NoSuchElementException-如果輸入已用盡

如果您包括了堆棧跟蹤,這對您和該論壇的成員將更加明顯。 嘗試放置anException.printStackTrace(); 在下一次您的捕獲塊中。

得到排序后,我有一個布爾變量,該變量不在正在讀取的文件中:),這一切都與休息並再次查看它有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM