[SOLVED] Stuck

• 03-30-2009, 02:36 AM
leonardjr
[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

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");     }         }```
• 03-30-2009, 02:41 AM
CJSLMAN
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:
Code:

`CompanyTotalWages = CompanyTotalWages + totalWages;`
Luck,
CJSL
• 03-30-2009, 02:47 AM
leonardjr
You are going to have to be more specific I am new to java.
• 03-30-2009, 02:59 AM
CJSLMAN
OK..... I'm assuming you know how to define a variable... something like:
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
• 03-30-2009, 03:12 AM
leonardjr
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
• 03-30-2009, 03:30 AM
Fubarable
Then you're doing it wrong. You may wish to post your new code.
• 03-30-2009, 03:39 AM
leonardjr
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");     }         }```
• 03-30-2009, 03:43 AM
Fubarable
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
Code:

`sumVariable = sumVariable + somethingElse;`
or this:
Code:

`sumVariable += somethingElse;`
(they do the same thing)
• 03-30-2009, 03:46 AM
leonardjr
How am I not intializing the variable
• 03-30-2009, 03:46 AM
leonardjr
excuse me declaring it
• 03-30-2009, 03:47 AM
Fubarable
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.
• 03-30-2009, 03:52 AM
leonardjr
umm ok i don't get it don't worry about it
• 03-30-2009, 03:59 AM
Fubarable
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);   } }```
• 03-30-2009, 04:18 AM
leonardjr
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");     }         }```
• 03-30-2009, 04:20 AM
Fubarable
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?
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.
• 03-30-2009, 04:32 AM
leonardjr
OMG it worked thank you so much!!!
• 03-30-2009, 04:35 AM
Fubarable
finally! Good show!