Coding style 读取、操作和输出数据时出错

Coding style 读取、操作和输出数据时出错,coding-style,Coding Style,问题已完成,无法删除帖子。我猜是因为您: while (inputFile.hasNext()) 使用Scanner.hasNextLine 编辑: 我用您的示例输入测试了您的代码。我现在明白你的意思了 while ( inputFile.hasNextLine() ) { employeeID = inputFile.nextLine(); // Read info from first line and store it in employeeID

问题已完成,无法删除帖子。

我猜是因为您:

 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)检查每次调用next时是否有下一个令牌。我认为(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文档的下一行。我不明白你在问我什么。它可以读取所有内容,但当有更多信息需要读取时,它会崩溃。您正在使用,然后您会调用两次。因此,最终您将得到一个“java.util.NoSuchElementException:找不到行”。这两种方法具有不同的分隔符。你正在逐行阅读,所以检查是否有新行。唯一的问题是我有如下数据:(第1行)00135(第2行)John Doe(第3行)10.50(第4行)0.20(第5行)empty(第6行)00179(第7行)Mary Brown等。。如果我打一次电话,在我的第二个循环中,它会显示员工的ID作为我的姓名。如果我打两次电话,它会在JOptionPane中正确显示员工的姓名,但随后我会得到error@Lee事实上,除非你修正了关于阅读的逻辑,否则你几乎肯定会得到这个错误。即使您进行了由DarkByte建议的更改,您也要检查文件中的一行,然后读取多个文件。(这会给你你看到的错误)。我认为您的“意思”是(在伪代码中)
while(file.hasNextLine()){//读一个单词,然后读另一个单词,等等}
,而这并不是您正在做的事情。您可能应该做的是检查一行,读取该行,然后用其他方法解析它。main中的第70行是什么?我猜文件的格式不正确