问题已完成,无法删除帖子。在读取数据,操作和输出时出错
回答
我猜那是因为你有:
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. 操作数据库输出
- 3. XML数据和C#,读入,操作,输出
- 4. 从Excel中读取数据时出错
- 5. 从数据库读取时出错
- 6. 读取整数时出错
- 7. 将数据从输入文件读取到数组时出错
- 8. Python代码在读取输入时出错并出错
- 9. 错误重载时输出操作
- 10. 在Mifare DESFire卡上读取和写入数据时出错
- 11. 从数据库中读取十进制值时输出错误
- 12. 读取串行数据传输时出错
- 13. 文件读取和输出错误
- 14. 获取操作时出现REstkit错误
- 15. 尝试使用executeUpdate发出数据操作语句时出错
- 16. 在读g数据时出错
- 17. 如何将show操作符的输出读回数据集?
- 18. 作为数据帧读取YAML文件时出错R
- 19. 数据读取器格式化输出
- 20. Javascript和jQuery获取数据时出错
- 21. 使用groovy读取输入xml和构造输出xml时出现错误
- 22. 尝试“Session :: instance();”时读取会话数据时出错在Kohana
- 23. 输出参数时出错
- 24. Docker化的应用程序抛出读取操作超时错误并退出
- 25. 在数据库行中读取时重复输出
- 26. C++在读取数据和作为类存储时出现故障。初始化时出错
- 27. 读取CC和BCC属性时出错
- 28. 在Mathematica模块中读取数据和返回输出
- 29. Appengine数据存储读取操作超出限制
- 30. XML数据在尝试在类内部读取时出错
是什么线主70?我猜它的文件格式不正确 –