0

This program is for calculating an employees net pay after deductions, but i am running into issues in displaying gross pay in the summary of the employees salary.

I have changed around the types of dialog boxes etc. , is there also a solution to allow me to use a non input dialog box in the summary of the payslip?

import javax.swing.JOptionPane; // Dialog boxes are created to allow user inputs
public class Payslipgenerator  
{


  public static void main(String[] args) 
  {
      String employeeName,grossPay,payslipFinal; //Declaring String variables
      double PRSI=.025, PAYE=.2, USC=.04, Pension=.05, healthInsurance= 75; //Declaring consants used in the Application
      double prsiCalc, payeCalc, uscCalc,pensionCalc, netValue; //Double variables that will store calculations

      employeeName = JOptionPane.showInputDialog(null, "Enter Employee name : ", "User Info"); //Asks Employee to input their name, which is then stored
      grossPay = JOptionPane.showInputDialog(null, "Enter Gross Pay (€) : ", "User Info"); //Prompts employee to enter Gross Pay for deduction calculation
      double employeeGrossPay = Double.parseDouble(grossPay); //Stores input in double Variable

      prsiCalc      = employeeGrossPay * PRSI;
      payeCalc      = employeeGrossPay * PAYE;
      uscCalc       = employeeGrossPay * USC ;
      pensionCalc   = employeeGrossPay * Pension ; 
      netValue      = employeeGrossPay - (prsiCalc + payeCalc + uscCalc + pensionCalc + healthInsurance);
      payslipFinal  = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
      + employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue); 

      JOptionPane.showInputDialog(null, "Employee Name : " + employeeName + "\n\n" + payslipFinal, "Payslip Breakdown");

  }
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
E.MAC
  • 11
  • 1
  • 2
    Just at a quick guess from looking, you have a `+` in the `String.format` call... Did you mean that to be a `,`? – BeUndead Oct 09 '19 at 12:53
  • Have a look at [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) for solving application crashes. – Markus Kauppinen Oct 09 '19 at 13:02

1 Answers1

0

Change

payslipFinal  = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
      + employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue); 

to

payslipFinal  = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
      , employeeGrossPay, prsiCalc, uscCalc, pensionCalc, netValue); 

Test:

public static void main(String[] args) {
    String payslipFinal  = String.format("Gross Pay :€%.2f \nPRSI : €%.2f \nUSC : €%.2f \nPension Plan : €%.2f \nHealth Insurance : €75.00 \nNet Earnings: €%.2f"
            , 1f, 2f, 3f, 4f, 5f);
    System.out.println(payslipFinal);
}

Output:

Gross Pay :€1,00 
PRSI : €2,00 
USC : €3,00 
Pension Plan : €4,00 
Health Insurance : €75.00 
Net Earnings: €5,00
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • The replacement of the + for a , is what helped fixed the app more or less, fresh eyes always help! I needed to display the output in a dialog box but was able to fix that myself thankfully, many thanks for your help! – E.MAC Oct 09 '19 at 13:57