Results 1 to 4 of 4
  1. #1
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Returning total from loop

    I have a for each loop that outputs the Mechanics salary for the first team in the arraylist, how can I add these figures together instead of having separate value for each?

    The code in my testing class to get the salary for mechanics:

    Java Code:
    	for (Mechanic str1: formula1.get(0)){
    			System.out.println(str1.getSalary());		
    		}
    How can I also get the salary for the driver 1 + 2 in the same team and add their salary to this?

    I have attaached an image showing the classes/fields created Returning total from loop-screenshot-2014-01-01-12.50.34.png

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,029
    Rep Power
    4

    Default Re: Returning total from loop

    In real life you would run over the list, add all the numbers and then have a total:
    Java Code:
    1024
    2000
    4010
    ====
    7034
    In Java it works the same way. Define a "total" variable, add all the numbers during the loop and bosh, a job well done.

    Edit: even summing these numbers can be done wrong... *sigh*
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    ahanslot is offline Member
    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0

    Default Re: Returning total from loop

    Quote Originally Posted by SurfMan View Post
    In real life you would run over the list, add all the numbers and then have a total:
    Java Code:
    1024
    2000
    4010
    ====
    7034
    In Java it works the same way. Define a "total" variable, add all the numbers during the loop and bosh, a job well done.

    Edit: even summing these numbers can be done wrong... *sigh*
    thanks for the reply thats the thing I get the concept I jsut dont know how to write the code:

    double total;
    total = ?

    ive got it

    Java Code:
    double total =0;
    		for (Mechanic str1: formula1.get(0)){
    			total += str1.getSalary();
    		}
    		System.out.println(total);
    just need to add the driver salary now
    Last edited by ahanslot; 01-07-2014 at 12:49 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,029
    Rep Power
    4

    Default Re: Returning total from loop

    There you go. *Thumbs up*
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. running total for loop structure.
    By downWithJava in forum New To Java
    Replies: 18
    Last Post: 03-04-2013, 11:22 PM
  2. min, max, total using arrays
    By Andie in forum New To Java
    Replies: 1
    Last Post: 03-15-2012, 08:35 AM
  3. Help returning value from loop?
    By ozzyman in forum New To Java
    Replies: 10
    Last Post: 04-11-2011, 01:28 PM
  4. Running total little help
    By silverspoon34 in forum Forum Lobby
    Replies: 9
    Last Post: 02-27-2011, 10:19 PM
  5. Printing total out
    By denisdoherty in forum New To Java
    Replies: 1
    Last Post: 04-25-2008, 07:40 AM

Posting Permissions

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