問題已完成,無法刪除帖子。在讀取數據,操作和輸出時出錯
回答
我猜那是因爲你有:
while (inputFile.hasNext())
使用Scanner.hasNextLine。
編輯:
我與樣品輸入測試你的代碼。我明白你的意思了。
while (inputFile.hasNextLine()) {
employeeID = inputFile.nextLine(); // Read info from first line and store it in employeeID
employeeName = inputFile.nextLine(); // Read info from next line and store it in employeeName
userInput = JOptionPane.showInputDialog("Employee Name: " + employeeName + "\nEnter number of" + // display employee name and ask for number of hours worked
" hours worked:");
hours = Double.parseDouble(userInput); // Store user's parsed input into hours
wageRate = inputFile.nextDouble(); // Read info from next line and store it in wageRate
taxRate = inputFile.nextDouble(); // Read info from next line and store it in taxRate
使用hasNextLine作爲你的情況只會確保對nextLine下一個電話將是有效的。但是,您的電話nextLine兩次,然後撥打nextDouble之後。您可以(1)確保您的電話與文件完全匹配,或者(2)每次您撥打下一個時檢查是否有下一個令牌。我認爲(1)是你的問題。
我能夠用下面的修復程序:
while (inputFile.hasNextLine()) {
employeeID = inputFile.nextLine();
employeeName = inputFile.nextLine();
userInput = JOptionPane.showInputDialog("Employee Name: " + employeeName + "\nEnter number of hours worked:");
hours = Double.parseDouble(userInput);
wageRate = Double.parseDouble(inputFile.nextLine());
taxRate = Double.parseDouble(inputFile.nextLine());
Paycheck paycheck = new Paycheck(employeeID, employeeName, wageRate, taxRate, hours);
paycheck.calcWages();
JOptionPane.showMessageDialog(null, "Employee ID: " +
paycheck.getEmployeeID() + "\nName: " +
paycheck.getEmployeeName() + "\nHours Worked: " +
hours + "\nWage Rate: $" +
money.format(paycheck.getWageRate()) + "\nGross Pay: $" +
money.format(paycheck.getGrossPay()) + "\nTax Rate: " +
paycheck.getTaxRate() + "\nTax Withheld: $" +
money.format(paycheck.getTaxWithheld()) + "\nNet Pay: $" +
money.format(paycheck.getNetPay()));
}
文件內容:
00135
John Doe
10.50
0.20
00179
Mary Brown
12.50
1.20
我正在檢查inputFile,它是我創建的Scanner類的實例,是否具有.txt文檔的下一行。我不明白你在問我什麼。它讀取一切正常,但是當有更多的信息要閱讀時,它會崩潰。 – Leon
您正在使用[hasNext()](http://download.oracle.com/javase/1,5,0/docs/api/java/util/Scanner.html#hasNext()),然後您致電[ nextLine()](http://download.oracle.com/javase/1,5,0/docs/api/java/util/Scanner.html#nextLine())兩次。所以最終你會得到一個「java.util.NoSuchElementException:No line found」。這兩種方法有不同的分隔符。您正逐行閱讀,因此請檢查新行。 – DarkByte
唯一的問題是,我有這樣的數據: (1號線)00135 (線2)John Doe的 (第3行)10.50 (線4)0.20 (線路5)*空* (第6行) 00179 (第7行)Mary Brown etc .. 如果我撥打
- 1. 操作數據庫輸出
- 2. XML數據和C#,讀入,操作,輸出
- 3. 讀取整數時出錯
- 4. Python代碼在讀取輸入時出錯並出錯
- 5. 在Mifare DESFire卡上讀取和寫入數據時出錯
- 6. 從數據庫中讀取十進制值時輸出錯誤
- 7. 讀取串行數據傳輸時出錯
- 8. 在讀g數據時出錯
- 9. 如何將show操作符的輸出讀回數據集?
- 10. 使用groovy讀取輸入xml和構造輸出xml時出現錯誤
- 11. 數據讀取器格式化輸出
- 12. 在數據庫行中讀取時重複輸出
- 13. C++在讀取數據和作爲類存儲時出現故障。初始化時出錯
- 14. 讀取CC和BCC屬性時出錯
- 15. 在Symfony中執行RabbitMQ使用者時出現「讀取數據時出錯」
- 16. 讀取文件時出錯
- 17. 讀取XML時出錯
- 18. RISC-V ISA輸入和輸出操作
- 19. 從cmd讀取輸出獲取錯誤
- 20. 命令在讀取時不輸出?
- 21. 讀取「svn log」命令輸出時出錯
- 22. 導出爲CSV時操作數據?
- 23. 讀取輸入和輸出結果
- 24. 在覈心數據中讀取時出錯
- 25. 在C#winforms中讀取csv文件時出現數據錯誤
- 26. 輸入/輸出操作 - ML
- 27. rrdtool讀取輸出
- 28. Bash讀取輸出?
- 29. 從數據庫讀取時出現NullPointerException
- 30. 從文件中使用python數組讀取數據時出錯
是什麼線主70?我猜它的文件格式不正確 –