0

new java developer here so please bear with me.

I'm currently creating a basic program which will take user inputs and store them in an array list. There are some support logs in an array list and I am trying to calculate the sum of total hours spent from all support log entries.

This is my array list and the records:

import java.util.Scanner;
import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);


    ArrayList<User> listOfUsers = new ArrayList<>();
    ArrayList<Customer> listOfCustomers = new ArrayList<>();
    ArrayList<Logs> listOfLogs = new ArrayList<>();


    listOfUsers.add(new User("janedoe@gmail.com", "Jane Doe", "testABC"));

    listOfLogs.add(new Logs(11, 24, 05, 2018, 3, 280.04));
    listofLogs.add(new Logs(12, 12, 09, 2018, 4, 290.11));

the fifth values in the list are the hours (3 & 4 respectively).

This is what I tried at the moment but it didn't work:

if (menuOption == 5) {

    double sum = 0;
    for(int i = 0; i < l.getHours(); i++)
    sum += l.get(i);
    return sum;

}

my getter is as follows:

public int getHours() {
    return this.hours;
}

I will appreciate all support, thanks in advance.

EDIT: So after some answers have been posted, I have managed to iterate over the lists and get the values of the hours as follows:

  if (menuOption == 5) {

    for(Logs aLog : listOfLogs) {

        double sum = 0;

        sum += aLog.getHours();

        System.out.println(sum);      
    }
  }

I get the following output:

3.0
4.0

How do I add the values so that they would show as a total of "7.0" ?

gmolaire
  • 1,091
  • 10
  • 19
AmsKumar
  • 93
  • 1
  • 10
  • 1
    Assuming `l` is set to `listOfLogs`, and your `getHours()` in inside the `Logs` class, your loop should look like: `for (Logs aLog : l) { sum += aLog.getHours() }` The array has Logs instances - so that is what you iterate over, and then you can do something (like get the hours) from/to each Logs instance... (and `hours` should probably be a `double`..) – moilejter Aug 26 '18 at 20:13
  • Hey, thanks for your reply. I kind of understand where you're coming from. I'll give it a shot and see if it works. Thank you. – AmsKumar Aug 26 '18 at 20:25
  • Hey, I tried your way and it works in terms of getting the hours. But I'm not sure how I should add the hours to get a total, do you know how to do this? – AmsKumar Aug 26 '18 at 22:08
  • 1
    You want to put the declaration of sum before the for loop - as it is, the variable is recreated on each iteration of the loop, so it never gets a chance to sum up anything. If you move the line `double sum = 0` to before the loop, that one variable `sum` will exist throughout the execution of the loop, and so it will accumulate all hours. – moilejter Aug 26 '18 at 22:48

2 Answers2

0

You could try this:

if (menuOption == 5) {

                        double sum = 0;
                        for(int i = 0; i < listOfLogs.size() ; i++){
                        sum += listOfLogs.get(i).getHours();
                        }
                        return sum;

                    }

Changes made:

In the for loop i<(size of the listOfLogs List) which contains the Logs Object. Then, you have to iterate through each Logs instance and get the hours and add that to sum.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • Hi Yug, thanks for your reply. I tried this and it iterated the list but gave me the following error: Main.java:27: error: integer number too large: 09 listofLogs.add(new Logs(12, 12, 09, 2018, 4, 290.11)); – AmsKumar Aug 26 '18 at 20:24
  • Is there anyway I can specifically target just the 'hours' value in the list? – AmsKumar Aug 26 '18 at 20:27
  • The list doesn’t contain the hours directly. The list contains the logs instances which in turn have the hours. So you have to get the logs and then through them you can get the hours. – Yug Singh Aug 26 '18 at 20:32
  • Could i extract the values from the log instance and create a seperate list? If so how would I do that? – AmsKumar Aug 26 '18 at 21:29
  • 1
    You are declaring ```double sum = 0;``` inside the for loop. So each time it is initializing to 0. Keep it outside the for loop. It will work. – Yug Singh Aug 27 '18 at 08:11
0

Keep the sum outside the loop. This will allow it to be updated. If it remains inside the loop, it will always be reset on every iteration:

if (menuOption == 5) {
  double sum = 0;
  for(Logs aLog : listOfLogs) {
      sum += aLog.getHours();   
  }
}
gmolaire
  • 1,091
  • 10
  • 19