Results 1 to 17 of 17

Thread: [SOLVED] Stuck

  1. #1
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [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");
        }
        
        
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    You are going to have to be more specific I am new to java.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Then you're doing it wrong. You may wish to post your new code.

  7. #7
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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");
        }
        
        
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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)

  9. #9
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    How am I not intializing the variable

  10. #10
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    excuse me declaring it

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  12. #12
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    umm ok i don't get it don't worry about it

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please look at this example:
    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);
      }
    }

  14. #14
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    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");
        }
        
        
    }

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  16. #16
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    OMG it worked thank you so much!!!

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 02:44 AM
  2. Stuck on code...plz help
    By trump101 in forum New To Java
    Replies: 1
    Last Post: 11-26-2008, 01:48 AM
  3. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 03:56 AM
  4. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 03:22 AM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 07:40 PM

Posting Permissions

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