1. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## [SOLVED] Stuck

I'm a little stuck, after it displays the last employees information I need it to display the total wages the company is paying but I'm not sure how to do that! Can someone help

Java Code:
```import java.util.Scanner;

public class FigureWages {

public static void main(String[] args) {
Scanner input=new Scanner(System.in);

//Recieve the number of employees
System.out.print("Enter number of employees ");
double numEmp=input.nextDouble();

for(int i=0; i<numEmp; i++){

//Enter and recieve employee number
System.out.print("Enter employee number ");
int empNum=input.nextInt();

//Enter and recieve base pay rate per hour
System.out.print("Enter employees base pay rate per hour ");
double basePay=input.nextDouble();

//Enter and recieve Number of hours worked
System.out.print("Enter numbers of hours worked ");
int numHour=input.nextInt();

//Calculate total wages
double totalWages=0;
if(numHour<=40){
totalWages=numHour*basePay;}
else if(numHour<=60){
totalWages=(40*basePay+(1.5*basePay*(numHour-40)));}
else {
totalWages=(40*basePay+20*(1.5*basePay)+(2*basePay*(numHour-60)));}

//Display all information
System.out.print("Employee number " + empNum + "\nNumber of hours worked "+ numHour +"\nTotal Wages " + totalWages+"\n");}

System.out.print("This program was written by Justeena Leonard");
}

}```  Reply With Quote

2. ## Define another variable to contain the company's total wages and at the very end just add to it the total wages to it with each loop:
Java Code:
`CompanyTotalWages = CompanyTotalWages + totalWages;`
Luck,
CJSL  Reply With Quote

3. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## You are going to have to be more specific I am new to java.  Reply With Quote

4. ## OK..... I'm assuming you know how to define a variable... something like:
Java Code:
`double CompanyTotalWages;`
Define it before the loop and at the end of your loop add the line of code from my previous post. After the loop just print the value of the companyTotalWages.

Luck,
CJSL  Reply With Quote

5. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## When i ran it in the program it doesn't add all the total wages from all the employees it just tells me how much they have to pay out for that one person  Reply With Quote

6. ## Then you're doing it wrong. You may wish to post your new code.  Reply With Quote

7. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## Java Code:
```import java.util.Scanner;

public class FigureWages {

public static void main(String[] args) {
Scanner input=new Scanner(System.in);

//Recieve the number of employees
System.out.print("Enter number of employees ");
double numEmp=input.nextDouble();

double companyTotalWages;
for(int i=0; i<numEmp; i++){

//Enter and recieve employee number
System.out.print("Enter employee number ");
int empNum=input.nextInt();

//Enter and recieve base pay rate per hour
System.out.print("Enter employees base pay rate per hour ");
double basePay=input.nextDouble();

//Enter and recieve Number of hours worked
System.out.print("Enter numbers of hours worked ");
int numHour=input.nextInt();

//Calculate total wages
double totalWages=0;
if(numHour<=40){
totalWages=numHour*basePay;}
else if(numHour<=60){
totalWages=(40*basePay+(1.5*basePay*(numHour-40)));}
else {
totalWages=(40*basePay+20*(1.5*basePay)+(2*basePay*(numHour-60)));}

//Display all information
System.out.print("Employee number " + empNum + "\nNumber of hours worked "+ numHour +"\nTotal Wages " + totalWages + "\n");
companyTotalWages = companyTotalWages + totalWages;}

System.out.print("\nThis program was written by Justeena Leonard");
}

}```  Reply With Quote

8. ## 1) You're not declaring your summation variable before the for loop as was recommended. A new variable will just be created each time the loop iterates, and that's not what you want to do.
2) You're not summing with this variable. To sum, you have to add to what's already held by the variable. In other words either this
Java Code:
`sumVariable = sumVariable + somethingElse;`
or this:
Java Code:
`sumVariable += somethingElse;`
(they do the same thing)  Reply With Quote

9. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## How am I not intializing the variable  Reply With Quote

10. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## excuse me declaring it  Reply With Quote

11. ## Please re-read what I wrote. I didn't say that you're not initializing the variable, or that you're not declaring it. I'm saying where you're declaring it. It must be before the for loop.  Reply With Quote

12. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## umm ok i don't get it don't worry about it  Reply With Quote

13. ## Java Code:
```public class Fubar4
{
public static void main(String[] args)
{
int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int i = 0; i < intArray.length; i++)
{
int sum1 = 0;
sum1 = intArray[i]; // I'm not adding here.
}
// sum1 doesn't even exist outside of this loop.

int sum2 = 0;  // declared before the next for loop
for (int i = 0; i < intArray.length; i++)
{
sum2 = sum2 + intArray[i];
}
// since sum2 was declared outside of the for loop
// it exists outside of the for loop
System.out.println("sum2 = " + sum2);
}
}```  Reply With Quote

14. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## Java Code:
```import java.util.Scanner;

public class FigureWages {

public static void main(String[] args) {
Scanner input=new Scanner(System.in);

//Recieve the number of employees
System.out.print("Enter number of employees ");
double numEmp=input.nextDouble();

double companyTotalWages;
double totalWages;
companyTotalWages=0;
for(int i=0; i<numEmp; i++){

//Enter and recieve employee number
System.out.print("Enter employee number ");
int empNum=input.nextInt();

//Enter and recieve base pay rate per hour
System.out.print("Enter employees base pay rate per hour ");
double basePay=input.nextDouble();

//Enter and recieve Number of hours worked
System.out.print("Enter numbers of hours worked ");
int numHour=input.nextInt();

//Calculate total wages
if(numHour<=40){
totalWages=numHour*basePay;}
else if(numHour<=60){
totalWages=(40*basePay+(1.5*basePay*(numHour-40)));}
else {
totalWages=(40*basePay+20*(1.5*basePay)+(2*basePay*(numHour-60)));}

companyTotalWages=+totalWages;
//Display all information
System.out.print("Employee number " + empNum + "\nNumber of hours worked "+ numHour +"\nTotal Wages " + totalWages + "\n");

}

System.out.println("Companies total wages is " + companyTotalWages);
System.out.print("\nThis program was written by Justeena Leonard");
}

}```  Reply With Quote

15. ## and it's still not adding up total wages, is it?

imagine that totalWages holds 20420.44, and then the for loop makes another loop for another worker. What happens to the 20420.44 held by totalWages when this line is processed?
Java Code:
`totalWages=(40*basePay+(1.5*basePay*(numHour-40)));`
it disappears. AGAIN, you have to add the new wages to the old wages. please re-read my example.  Reply With Quote

16. Member Join Date
Feb 2009
Posts
42
Rep Power
0

## OMG it worked thank you so much!!!  Reply With Quote

17. ## finally! Good show!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•