How to calculate sum of values stored in getter?

Tags: ,



I want to be able to calculate the sum of the int values stored in my getter method. So basically what I want the program to do is to sum the total hours of all the workers into one number. I have no idea how to do this…

The output I get with this code is just the hours for all the workers but not calculated into one sum.

for(Employee employee : emloyeeArr) {
  if(employee != null) {                
    System.out.println("Total hours for all the employees:" + 
          employee.getEmployeeHours());
  }
}

Answer

float total_Hours = 0;
for(Employee employee : emloyeeArr) {
 if(employee != null) 
 {
  total_Hours += employee.getEmployeeHours();                      
 }
 System.out.println("Total hours for all the employees:" + 
  total_Hours);

All you need to do is run a for loop, get each employee hour and add it in the total then display the total_Hours variable.



Source: stackoverflow