Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm guessing it's because you have:</p> <pre><code> while (inputFile.hasNext()) </code></pre> <p>Use <em>Scanner.hasNextLine</em>.</p> <p><strong>Edit</strong>:</p> <p>I tested your code with your sample input. I see what you mean now. </p> <pre><code>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 </code></pre> <p>Using <em>hasNextLine</em> as your condition will only ensure that the next call to <em>nextLine</em> will be valid. But, your calling <em>nextLine</em> twice, and then calling <em>nextDouble</em> after that. You can either (1) ensure that the calls your making match up with the file exactly, or (2) check that there is a next token every time you call <em>next</em>. I think (1) is your problem.</p> <p>I was able to fix your program with the following:</p> <pre><code>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() ) ); } </code></pre> <p>The file contents:</p> <pre><code>00135 John Doe 10.50 0.20 00179 Mary Brown 12.50 1.20 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload