trying to make a payroll program, im pretty sure this is all the code i need. maybe 1 or 2 maybe 3 extra lines.

ERRORS:
operator * cannot be applied to java.lang.String
operator * cannot be applied to double,java.lang.String
operator * cannot be applied to double,java.lang.String

ERRORS in CODE are Highlighted in red...


I try to do a line like this

double grossPayValue = hours * hourlyRate;
       double grossPay = Double.parseDouble(grossPayValue);

and it doesn't fix the problem...

Any suggestions????

package lab12payroll;
import javax.swing.JOptionPane;

public class Main {
    public static void main(String[] args) {
        //ask the user for input
      String name = JOptionPane.showInputDialog(null, "Please enter a name;");
      String hours = JOptionPane.showInputDialog(null, "Please enter the hours worked:");
      String hourlyRate = JOptionPane.showInputDialog(null, "Please enter the Hourly Rate:");
      String fedTaxRate = JOptionPane.showInputDialog(null, "Please enter the Federal Tax Rate Witholdings:");
      String stateTaxRate = JOptionPane.showInputDialog(null, "Please enter the State Tax Rate Witholdings:");
      
      //calc grossPay
      double grossPay = hours * hourlyRate;
      
      //calc fedTaxWitholding
      double fedTaxWitholding = grossPay * fedTaxRate;

      //calc stateTaxWitholding
      double stateTaxWitholding = grossPay * stateTaxRate;
      //calc totalDeductions
      double totalDeduc = fedTaxWitholding + stateTaxWitholding;

      //calc netPay
      double netPay = grossPay - totalDeduc;

      //Output in a dialog messageBox
      String var11 = JOptionPane.showInputDialog("Hours Worked: " + hours + "Pay Rate: " + hourlyRate + "Gross Pay: " + grossPay +
              "Deductions: " + "Federal Witholdings: " + fedTaxWitholding + "State Witholdings: " + stateTaxRate +
              "Total Deductions: " + totalDeduc + "Net Pay: " + netPay);
    }

}

the hours and hourlyRate variables are declared as character strings, you cannot multiply them.

You have to convert them first by parsing them using the Double.parseDouble() method.

double grossPay = Double.parseDouble(hours) * Double.parseDouble(hourlyRate);

but be careful if the strings are empty, because your code will crash in this case.

This article has been dead for over six months. Start a new discussion instead.