0
import java.util.Scanner;
import java.io.*;
public class GardinierPayrollP2
{
  public static void main(String[] args) throws IOException
 {
  // int id;  //i.d. number
  // double hrsworkd; //hours worked
   double wkspay = 0.00;  //total amount before tax for an individual employee
   double netpay = 0.00; //net pay to an individual after tax
   double runningTotal = 0.00; //total amount of paid salaries
   double runningNetTotal = 0.00; //net total after tax 
   double runningTaxTotal = 0.00; //total taxes payed 
   double levelA = 12.00;
   double levelB = 14.50;
   double levelC = 16.00;
   double levelD = 20.00;
   //String name; //employees First Name
   //char level; //level of payment for employee
   final double taxRate = .08; //tax rate @ 8%
   double taxes = 0.00; //dollar amount of taxes payed
  
  
  File employees = new File("employees.txt");
  Scanner inputFile = new Scanner(employees);
  
  
  
  while (inputFile.hasNext())
  {
    
   String name = inputFile.nextLine();
   int id = inputFile.nextInt();
   char level = inputFile.next().charAt(0);
   double hrsworkd = inputFile.nextDouble();
   
   System.out.println(name + id + level + hrsworkd);
  }

 }
}

This is what I'm working with. Second year of computer science. Not very versed in troubleshooting the errors yet. I know theres an input error. I just can't tell why. DrJava's output for it is to print the first four lines of the info and then throw the error. heres the output

> run GardinierPayrollP2
Rose Nylund901A10.0
java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at GardinierPayrollP2.main(GardinierPayrollP2.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>

Here's the input file

Rose Nylund
901
A
10.0
Dorothy Zbornak
534
D
11.5
Blanche Deveraux
109
B
5.0
Sophia Petrillo
729
C
2.5

What am I doing wrong?|

  • What line of your code is it throwing the error on? – Matt Busche Nov 22 '20 at 04:22
  • it's a run time error i guess? After i run it it prints the first while loop of data and then errors out – Phill Gardinier Nov 22 '20 at 04:24
  • Your output prints each element on a new line, but your code shows that the data for each person should be on one line, smooshed together. – NomadMaker Nov 22 '20 at 04:56
  • I'm testing the program to see if it will print all the info with in the input file. I can't seem to get it to execute the while loop more than once. it will print the first employees info then it sends the error. When I changed it to String name = inputFile.next(); instead of String name = inputFile.nextLine(); it printed nothing but the error no data from the input file at all – Phill Gardinier Nov 22 '20 at 05:03
  • `nextLine()` is consuming the remainder (empty) of the line after the `nextDouble()`. – chrylis -cautiouslyoptimistic- Nov 22 '20 at 05:14
  • Is there something I should use other than nextLine() or nextDouble() to make it return more data? – Phill Gardinier Nov 22 '20 at 05:34

1 Answers1

0
  1. Make sure to close inputFile at the end of processing.
  2. Do not mix inputFile.hasNext() with inputFile.nextLine() and inputFile.nextInt() i.e. if you want to use inputFile.nextLine(), you should test its corresponding hasNextXXX i.e. inputFile.hasNextLine(). Also, I would use just inputFile.nextLine() and parse the line into int or double as per the requirement because of the problems discussed here.

Demo:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File employees = new File("employees.txt");
        Scanner inputFile = new Scanner(employees);
        String name = null;
        int id = 0;
        char level = 0;
        double hrsworkd = 0;
        while (inputFile.hasNextLine()) {
            name = inputFile.nextLine();
            if (inputFile.hasNextLine()) {
                id = Integer.parseInt(inputFile.nextLine());
            }
            if (inputFile.hasNextLine()) {
                level = inputFile.nextLine().charAt(0);
            }

            if (inputFile.hasNextLine()) {
                hrsworkd = Double.parseDouble(inputFile.nextLine());
            }
            System.out.println(name + "," + id + "," + level + "," + hrsworkd);
        }
        inputFile.close();
    }
}

Output:

Rose Nylund,901,A,10.0
Dorothy Zbornak,534,D,11.5
Blanche Deveraux,109,B,5.0
Sophia Petrillo,729,C,2.5
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72