Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble running a java program and would appreciate some help. Here

ID: 3569709 • Letter: I

Question

I am having trouble running a java program and would appreciate some help.

Here is the program:

import java.io.*;
import java.text.*;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Employees_3_10 {
  
    final static double FEDTAX = .18;
    final static double STTAX = .045;

   static Toolkit tools = new Toolkit();

   public static void main (String[]args) throws Exception {

   Scanner input = new Scanner(System.in);

      // Define file names
      final String INPUT_FILE = "Employees_3_10_Input.txt";
      final String OUTPUT_FILE = "Employees_3_10_Output.txt";

      // Access the input/output files
        Scanner InputFile = new Scanner(new FileReader (INPUT_FILE));
        Scanner InputFileCount = new Scanner(new FileReader (INPUT_FILE));
        PrintWriter OutputFile = new PrintWriter(OUTPUT_FILE);
        int lines = countLines(InputFileCount);
        InputFileCount.close();


      // Define Variables
    
      double [][] data = new double [30][7];
      String [] names = new String [30];
      names = new String[lines];
      data = new double[lines][2];
      


        input(data, names, InputFile);
        printHeading(OutputFile);
        printEmployeeLines(names, data, OutputFile);
        printSummary(names, data, OutputFile);

        sortNamesAlpha(names, data);
        printHeading(OutputFile);
        printEmployeeLines(names, data, OutputFile);
        printSummary(names, data, OutputFile);

        sortGrossPay(names, data);
        printHeading(OutputFile);
        printEmployeeLines(names, data, OutputFile);
        printSummary(names, data, OutputFile);

        OutputFile.close();
}


//************************************************** ****************************************
public static int input(double [][] data, String [] names, Scanner inputFile)
{
int i = 0;
int m = 0;
String line;
StringTokenizer st;
while (inputFile.hasNext() && i < names.length)
{
line = inputFile.nextLine();
st = new StringTokenizer(line);
m = st.countTokens();
names [i] = st.nextToken();
for (int j = 1; j < m-2; j++)
names [i] = names [i] + " " + st.nextToken();
data [i][5] = Double.parseDouble(st.nextToken());
data [i][6] = Double.parseDouble(st.nextToken());
i++;}
return i;
}  

//*******************************************************************************************
public static void getName(String[] names, int i, StringTokenizer st){
        int m = st.countTokens();
            names[i] = st.nextToken();
            for (int j = 1; j < m - 2; j++){
            names[i] = names[i] + " " + st.nextToken();
            }
}             
//*******************************************************************************************
     public static int countLines(Scanner InputFile){
        String tmp;
        int i = 0;
        while (InputFile.hasNext()){
            tmp = InputFile.nextLine();
            i++;
        }// end while
        return i;
    }

//*******************************************************************************************
   public static double roundNumber(double valueToRound, int placesToRound) {

      double roundMe;     // The number to round, starts as valueToRound
      double powerOfTen; // To calculate 10^placesToRound
    
      roundMe = valueToRound;                      // Ex: round 1.256, 2 places
      powerOfTen = Math.pow(10,placesToRound);     // 10 ^ 2 = 100
      roundMe = roundMe * powerOfTen;              // 1.256 * 100 = 125.6
      roundMe = Math.round(roundMe);               // 125.6 rounded = 126
    
      return roundMe / powerOfTen;                 // 126 / 100 = 1.26
   } // End roundNumber

//******************************************************************************************
    public static void printHeading(PrintWriter OutputFile){
        int widthR = 17;
        int widthL = 12;
        OutputFile.println("|---------------------------------------------------" +
                "------------------------------------------|");
        OutputFile.print(rightPadName("Name", widthR));
        OutputFile.print(leftPad("Net Pay", widthL));
        OutputFile.print(leftPad("Gross Pay", widthL));
        OutputFile.print(leftPad("Federal Tax", widthL));
        OutputFile.print(leftPad("State Tax", widthL));
        OutputFile.print(leftPad("Hours", 8));
        OutputFile.print(leftPad("Pay Rate|", 13));
        OutputFile.print(" ");
        OutputFile.println("|---------------------------------------------------" +
                "------------------------------------------|");
    }// end print Heading
//************************************************** ****************************************      
    public static void printEmployeeLines(String[] names, double[][] data, PrintWriter OutputFile){
    //name, net pay, gross pay, fed tax, state tax, dues, hours, pay rate
        for (int i=0; i< names.length; i++){
            int widthR = 17;
            int widthL = 12;
            String name = names[i];
            OutputFile.print(rightPadName(name, widthR));
            OutputFile.print(leftPadCurrency(calcNetPay(data, i), widthL));
            OutputFile.print(leftPadCurrency(calcGrossPay(data, i), widthL));
            OutputFile.print(leftPadCurrency(calcFedTax(data, i), widthL));
            OutputFile.print(leftPadCurrency(calcStTax(data, i), widthL));
            OutputFile.print(leftPad(data[i][0], 8));
            OutputFile.print(leftPadCurrency(data[i][1], widthL));
            OutputFile.print("| ");
        }// end for
}
//************************************************** ****************************************
    public static double calcNetPay(double[][] data, int i){
        return (calcGrossPay(data, i) - calcFedTax(data, i) -
                calcStTax(data, i));
    }// end clac net pay

//*******************************************************************************************
public static double calcGrossPay(double[][] data, int i){
        if (data[i][0] <= 40.0){
            return (data[i][0] * data[i][1]);
        }
        else if (data[i][0] < 50){
            return (((data[i][0] - 40) * 1.5 * data[i][1]) + data[i][1]*40);
        }
        else if (data[i][0] >= 50){
            return (((data[i][0] - 50) * 2 * data[i][1]) + data[i][1] * 40 +
                    15 * data[i][1]);
        }
        else{
            return 0;
        }
    }// end calc GrossPay

//************************************************** ****************************************
public static double calcFedTax(double[][] data, int i){
        return (calcGrossPay(data, i) * FEDTAX);
    }// End calcFedTax
    public static double calcStTax(double[][] data, int i){
        return (calcGrossPay(data, i) * STTAX);
    }// end calcStTax

    public static void sortNamesAlpha(String[] names, double[][] data){
        for(int i = 0; i < names.length; i++){
            for (int j = 0; j < data.length; j++){
                if(names[i].compareTo(names[j]) > 0){
                    swapValues(data, i, j);
                    swapNames(names, i, j);
                }//end if
            }// end for j
        }// end for i
    }// end sortNamesAlpha
//*******************************************************************************************
    public static void sortGrossPay(String[] names, double[][] data){
        for(int i = 0; i < data.length; i++) {
            for(int j = i; j < data.length; j++) {
                if(calcGrossPay(data, i) > calcGrossPay(data, j)){
                    swapValues(data, i, j);
                    swapNames(names, i, j);
                }// end if
            }// end for
        }// end for
    }// end sortGrossPay
//******************************************************************************************
    public static void swapValues(double[][] data, int i, int j) {
        double temp = data[i][0];
        data[i][0] = data[j][0];
        data[j][0] = temp;
        double temp2 = data[i][1];
        data[i][1] = data[j][1];
        data[j][1] = temp2;
    }// end swapValues
//******************************************************************************************
    public static void swapNames(String[] names, int i, int j) {
        String temp = names[i];
        names[i] = names[j];
        names[j] = temp;
    }// end swap names
//******************************************************************************************
    public static void printSummary(String[] names, double[][] data, PrintWriter OutputFile){
        int widthR = 17;
        int widthL = 12;
        OutputFile.println("|-----------------------------------------------------"+
                "----------------------------------------|");
        OutputFile.print(rightPadName("Total", widthR));
        OutputFile.print(leftPadCurrency(calcTotalNetPay(data), widthL));
        OutputFile.print(leftPadCurrency(calcTotalGrossPay(data), widthL));
        OutputFile.print(leftPadCurrency(calcTotalFedTax(data), widthL));
        OutputFile.print(leftPadCurrency(calcTotalStTax(data), widthL));
        OutputFile.print(leftPad(calcTotalHours(data), 8));
        OutputFile.print("            | ");
        OutputFile.println("|-----------------------------------------------------"+
                "----------------------------------------|");
    }// end print summary
//********************************************************************************************
public static double calcTotalNetPay(double[][] data){
        double temp = 0;
        for(int i = 0; i < data.length; i++){
            temp += calcNetPay(data, i);
        }
        return temp;
    }// end calc total net pay
//********************************************************************************************
    public static double calcTotalGrossPay(double[][] data){
        double temp = 0;
        for( int i = 0; i < data.length; i++){
            temp += calcGrossPay(data, i);
        }
        return temp;
    }// end calc total gross pay
//*********************************************************************************************
    public static double calcTotalHours(double[][] data){
        double temp = 0;
        for(int i = 0; i< data.length; i++){
            temp += data[i][0];
        }
        return temp;
    }// end calc total hours
//*********************************************************************************************
    public static double calcTotalFedTax(double[][] data){
        double temp = 0;
        for( int i = 0; i < data.length; i++){
            temp += calcFedTax(data, i);
        }
        return temp;
    }// end calc total fed tax
//**********************************************************************************************
    public static double calcTotalStTax(double[][] data){
        double temp = 0;
        for( int i = 0; i < data.length; i++){
            temp += calcStTax(data, i);
        }
        return temp;
    }// end calc total st tax
//**********************************************************************************************
    public static String leftPadCurrency(double num, int width){
        DecimalFormat fmt = new DecimalFormat("#,##0.00");
        String s = fmt.format(num);
        int m = s.length();
        for(int i=0; i < width - m; i++){
            if(m == 5 && i == 3 || m == 4 && i == 4 || m == 8 && i == 0){
                s = "$" + s;
            }
            else if (m == 6 && i == 2)
                s = "$" + s;
            else{
                s = " " + s;
            }
        }
        return s;

    } // end leftPadCurrency
//**********************************************************************************************
    public static String leftPadDues(double num, int width){
        DecimalFormat fmt = new DecimalFormat("#,##0.00");
        String s = fmt.format(num);
        int m = s.length();
        for(int i=0; i < width - m; i++){
            if(m == 4 && i == 2 || m == 6 && i == 0){
                s = "$" + s;
            }
            else{
                s = " " + s;
            }

        }
        return s;
    }
    // left pad takes numbers
//***********************************************************************************************
    public static String leftPad(double num, int width){
        DecimalFormat fmt = new DecimalFormat("#,##0.0");
        String s = fmt.format(num);
        int m = s.length();
        for(int i=0; i < width - m; i++){
                s = " " + s;
        }
        return s;

    } // end leftPad
//***********************************************************************************************
    // overloaded leftPad takes string
    public static String leftPad(String s, int width){
        int m = s.length();
        for(int i=0; i < width - m; i++){
                s = " " + s;
        }
        return s;
    } // end leftPad overloaded
//***********************************************************************************************
    public static String rightPadName(String s, int width){
        int m = s.length();
        for(int i=0; i < width - m; i++){
            s = s + " ";
        }
        s = "|" + s;
        return s;
    }// end rightPadName
}

Here is what it says when I try to run the program:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Hancock"
   at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
   at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
   at java.lang.Double.parseDouble(Double.java:538)
   at Employees_3_10.input(Employees_3_10.java:73)
   at Employees_3_10.main(Employees_3_10.java:39)
I know the problem is on line 73

data [i][5] = Double.parseDouble(st.nextToken());

but what do I need to change it to to run?

Here is my input file just in case.

41.00 11.74 Hancock John
50.00 10.60 Light Karen L     
52.00 10.80 Fagan Bert Todd   
62.00 12.24 Antrim Forrest N  
38.40 14.74 Camden Warren     
44.33 12.04 Mulicka Al B
41.75 13.40 Lee Phoebe
24.00 11.40 Bright Harry
41.00 10.40 Garris Ted
43.00 12.00 Benson Martyne
31.90 12.40 Lloyd Jeanine D
44.00 13.50 Leslie Bennie A
48.40 14.40 Brandt Leslie
42.00 12.90 Schulman David
50.10 10.84 Worthington Dan
70.40 12.66 Hall Gus W
40.10 12.74 Prigeon Dale R    
43.00 12.44 Fitzgibbons Rusty
50.00 12.24 Feistner Merle S
23.00 12.34 Hallquist Dottie
43.33 10.90 Bolton Seth
43.00 12.20 Taylor Gregg
42.00 12.94 Raskin Rose          
50.10 12.44 Kenwood Pat
44.33 14.64 Slaughter Lew

Explanation / Answer

Its because you are you are trying to convert a string to a decimal number in the method

public static int input(double[][] data, String[] names, Scanner inputFile) .

Heres the correct method, replace and compile.

comment if you have any doubts

public static int input(double[][] data, String[] names, Scanner inputFile) {
       int i = 0;
       String line;
       StringTokenizer st;
       while (inputFile.hasNext() && i < names.length) {
           line = inputFile.nextLine();
           st = new StringTokenizer(line);
           data[i][0] = Double.parseDouble(st.nextToken());
           data[i][1] = Double.parseDouble(st.nextToken());

           names[i] = st.nextToken();
          
           i++;
       }
       return i;
   }