0

I made this sample program for a while, and I'm figuring out how to add the sum of the two employees' pay I inputted in the array. Any suggestions?

import javax.swing.JOptionPane;
public class Sample {
public static void main (String[] args)

{
    String Name;
    int i, HoursWorked, Rate, Pay=0, TotalPay=0, GrandTotalPay=0;
    int CivStatus=0, Single=500, Married=1000;

    System.out.println("Name\t\t\t\tCivil Status Code\t\t\tPay");

    int [] Employees = new int [2];
    for (i=0; i<Employees.length; i++)
    {
        Name=JOptionPane.showInputDialog("Enter your name: ");
        HoursWorked=Integer.parseInt(JOptionPane.showInputDialog("Enter hours worked: "));
        Rate=Integer.parseInt(JOptionPane.showInputDialog("Enter hourly rate: "));
        Pay=HoursWorked*Rate;
        CivStatus=Integer.parseInt(JOptionPane.showInputDialog("Enter your civil status: \nEnter [1] if single.\nEnter [2] if married."));

        if(CivStatus==1){
            {
                TotalPay=Pay-Single;
            } 
        }
        if(CivStatus==2){
            {
                TotalPay=Pay-Married;
            }
        }
        GrandTotalPay=Employees[0]+Employees[1];
        System.out.println(Name+"\t\t\t\t"+CivStatus+"\t\t\t\t\t"+Pay);
    }
    System.out.println("The sum of the pay is: "+GrandTotalPay);
    }
}

4 Answers 4

1

Array index Out of bounds exception occurs when you try to access a memory location not present in the array. You are doing same in

for (i=0; i<=Employees.length; i++)

Instead, do

for (i=0; i<Employees.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

1

After you calculate each Pay, add it to TotalPay. Like:

TotalPay = TotalPay + Pay;

Or,

TotalPay += Pay;

The next thing, you actually are not using the array here. If you want to keep the Pays in the array, you need an array of size 2.

int[] Employees = new int[2];

Another thing, when traversing an array, do it like this:

for(i = 0; i < Employees.Length; i++) {}

Array indexing starts from 0, and goes up to (size - 1), so you cannot use <= Employees.Length here.

Comments

0

Array index starts from 0. If you want to loop through all elements use indices from 0 till length. Change i<=Employees.Length into i<Employees.Length

8 Comments

Thanks! I did as you asked, but how will I be able to add the sum of the two employees' pay?
In which data structure you having the pay details. Array right? then add another loop for adding the pay
@BrianIrvin accept the answer if its working. for further assistance update question with the code you've right now for adding the pay details.
I have updated the code I posted. I'm still lost on finding the sum of the two arrays. What do I put? :(
Adding arrays a[length] and b[length] for(i=0;i<length;i++) { sum=a[i]+b[i] } change array names as you having.
|
0

Check the code below and make changes. Note: changed Pay into an Array for holding pay of each employees

import javax.swing.JOptionPane;
public class Sample {
public static void main (String[] args)
{
    String Name;
    int i, HoursWorked, Rate, TotalPay=0, GrandTotalPay=0;
    int CivStatus=0, Single=500, Married=1000;

System.out.println("Name\t\t\t\tCivil Status Code\t\t\tPay");
int [] Pay = new int [2];
int [] Employees = new int [2];
for (i=0; i<Employees.length; i++)
{
    Name=JOptionPane.showInputDialog("Enter your name: ");
    HoursWorked=Integer.parseInt(JOptionPane.showInputDialog("Enter hours worked: "));
    Rate=Integer.parseInt(JOptionPane.showInputDialog("Enter hourly rate: "));
    Pay[i]=HoursWorked*Rate;
    CivStatus=Integer.parseInt(JOptionPane.showInputDialog("Enter your civil status: \nEnter [1] if single.\nEnter [2] if married."));

    if(CivStatus==1){
        {
            TotalPay=Pay[i]-Single;
        } 
    }
    if(CivStatus==2){
        {
            TotalPay=Pay[i]-Married;
        }
    }
    GrandTotalPay+=Pay[i];
    System.out.println(Name+"\t\t\t\t"+CivStatus+"\t\t\t\t\t"+Pay[i]);
}
System.out.println("The sum of the pay is: "+GrandTotalPay);
    }
}

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.