programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Tim Cooke
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Paul Clapham
• Rob Spoor
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Piet Souris
• Carey Brown
Bartenders:

# What is the best way of calculating average for a specific column of an arraylist

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
Let's say we have 2 columns as below

and We can Call the values of Column 1 and 2 by a getter through a for each loop. Now I want the average of each column, so what is the best way for adding up the values and diving them ? I was thinking of something like this

and the same thing for the other column C2 but I think there is something wrong with this.

Bartender
Posts: 1464
32
• 1
• Number of slices to send:
Optional 'thank-you' note:
saeid, what kind of objects are you storing in that arraylist? Your list of the contents shows eight entries for C1, but only six for C2. The arraylist only has one size, so if you have eight entires for C1, I think there have to be eight for C2 as well.

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

Stevens Miller wrote:saeid, what kind of objects are you storing in that arraylist? Your list of the contents shows eight entries for C1, but only six for C2. The arraylist only has one size, so if you have eight entires for C1, I think there have to be eight for C2 as well.

yes you are right. just a mistype

lowercase baba
Posts: 13086
67
• 1
• Number of slices to send:
Optional 'thank-you' note:
How would YOU calculate the average? Forget about java. Pretend you are explaining things to a child, using English (or any natural language of your choice). Write down the steps, revise them, make them clearer and simpler.

Once you've done that, writing the java code should be much easier.

For example, if you were trying to find the average of ten number, how many times would you use division? In your example code, you are using division once for every item in the collection..that doesn't seem right, does it?

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

fred rosenberger wrote:How would YOU calculate the average? Forget about java. Pretend you are explaining things to a child, using English (or any natural language of your choice). Write down the steps, revise them, make them clearer and simpler.

Once you've done that, writing the java code should be much easier.

For example, if you were trying to find the average of ten number, how many times would you use division? In your example code, you are using division once for every item in the collection..that doesn't seem right, does it?

yea but if I take it out of the for loop then I don't know the what to divide them by! in this case it will be divided by 8 but in my actual file i don't know the length of each column in each file. before this I need to know how to add up the values of a column.
This is the first time I'm working with arrayList if it is just the array itself I am more comfortable with that.

Saloon Keeper
Posts: 9831
80
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:

fred rosenberger wrote:How would YOU calculate the average? Forget about java. Pretend you are explaining things to a child, using English (or any natural language of your choice). Write down the steps, revise them, make them clearer and simpler.

Once you've done that, writing the java code should be much easier.

For example, if you were trying to find the average of ten number, how many times would you use division? In your example code, you are using division once for every item in the collection..that doesn't seem right, does it?

yea but if I take it out of the for loop then I don't know the what to divide them by! in this case it will be divided by 8 but in my actual file i don't know the length of each column in each file. before this I need to know how to add up the values of a column.
This is the first time I'm working with arrayList if it is just the array itself I am more comfortable with that.

When dealing with a Java class that you've never used before it's a good idea to look through the methods it provides in the Javadoc. http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

fred rosenberger
lowercase baba
Posts: 13086
67
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:
yea but if I take it out of the for loop then I don't know the what to divide them by! in this case it will be divided by 8 but in my actual file i don't know the length of each column in each file. before this I need to know how to add up the values of a column.
This is the first time I'm working with arrayList if it is just the array itself I am more comfortable with that.

How do you know inside the loop what to divide by? Is there some way you could get that value outside the loop? if it is something dynamically generated inside the loop (i.e. you only count some of the items, is there a way you could still have that value in scope outside the loop?

Bartender
Posts: 10780
71
• 1
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:yea but if I take it out of the for loop then I don't know the what to divide them by! in this case it will be divided by 8 but in my actual file i don't know the length of each column in each file. before this I need to know how to add up the values of a column.

OK, so tackle THAT problem first. Write a program that can display you the number of values in each column.

Programming is all about breaking down tasks and dealing with them incrementally.

This is the first time I'm working with arrayList if it is just the array itself I am more comfortable with that.

Well, I don't see anything terrible you've done so far. Here are two hints for you though.
1. All Java Lists have a size() method.
2. Java Lists are also Iterables, which means you can use the "enhanced" for loop (shown near the bottom of the page).

HIH

Winston

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
Thank you guys for all your answers. Let me be more specific about my problem. I add up all the values in the column which is the sum but the way I'm doing it is using a loop and if my values in a column are 1, 2, 3. it will print
the sum is 1.
the sum is 3.
the sum is 6.

because it is in a loop and the same thing happens to my average. it divides each of these sums with the number of iterator. While I only need the last value of sum and last value of iterator to be able to calculate the average. I can't take them out of loop because my program has a detector that detects any change of value in C1 then prints the value of C2 until the value of C1 changes and each time it calculates the sum(and I'm trying to calculate average eventually) So basically I am calculating several average in each column.
this is what I've done:

Marshal
Posts: 77157
370
• 1
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:. . .

What does that do? Shouldn't it read
System.out.println();?

Winston Gutkowski
Bartender
Posts: 10780
71
• 2
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:While I only need the last value of sum and last value of iterator to be able to calculate the average. I can't take them out of loop because my program has a detector that detects any change of value in C1 then prints the value of C2 until the value of C1 changes

Well I don't see any evidence of it in the code you posted.

Why don't we do it this way? Right now, your code appears to deal with two things: year and month (which would have been useful to know right at the start).

For EACH iteration of the loop - ie. for every element in your list:
1. If the "current" year is not equal to the one from the last iteration, you:
• print a separator
• set your running month total to 0
• print out the current year
• 2. You add the current month value to your running month total.
3. You set the year to the current year.
4. Print out the running month total.
5. Print out a spacer line.

Now is THAT what you want to happen? Do you really want to set the year, or print out the month total, on every iteration?
If not, explain to us IN ENGLISH what you do want to happen.

Also, you are now printing out many totals, not just one. So if you want to convert that to an average, what do you think you'll have to do?

Right now, I suspect you're too bogged down in the mechanics of this problem and you can't see the wood for the trees.

Winston

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

Winston Gutkowski wrote:

saeid jamali wrote:While I only need the last value of sum and last value of iterator to be able to calculate the average. I can't take them out of loop because my program has a detector that detects any change of value in C1 then prints the value of C2 until the value of C1 changes

Well I don't see any evidence of it in the code you posted.

Why don't we do it this way? Right now, your code appears to deal with two things: year and month (which would have been useful to know right at the start).

For EACH iteration of the loop - ie. for every element in your list:
1. If the "current" year is not equal to the one from the last iteration, you:
• print a separator
• set your running month total to 0
• print out the current year
• 2. You add the current month value to your running month total.
3. You set the year to the current year.
4. Print out the running month total.
5. Print out a spacer line.

Now is THAT what you want to happen? Do you really want to set the year, or print out the month total, on every iteration?
If not, explain to us IN ENGLISH what you do want to happen.

Also, you are now printing out many totals, not just one. So if you want to convert that to an average, what do you think you'll have to do?

Right now, I suspect you're too bogged down in the mechanics of this problem and you can't see the wood for the trees.

Winston

Sorry for the confusion. Yes that's actually what I want except for a few things. so this code gives the following out put :
enter the city
-------------
1920
Sum of month is:3
Sum of month is:7
Sum of month is:12
Sum of month is:18
Sum of month is:25
Sum of month is:33
Sum of month is:42
-------------
1921
Sum of month is:1
-------------
1922
Sum of month is:2
Sum of month is:5
Sum of month is:9
Sum of month is:14

Of course I don't need to print the sum it's just for me to figure out how it works. the reason I have sum is to calculate the average. Everything is good except for the fact that I don't want my average to be calculated line by line for every single of those sums above. I want to divide that 42 by the number of iterate to that point which 7 and print it out only once and again for year 1922 I want to divide 14 which is the total sum of that year by the number of iterate to that point which is 4 and print the result only once. I added MonthAV=sumMonth/i; and printed it before my line separator in my if statement but it gave an exception: can not divide by 0. I added one more if statement below my current if statement if(i !=0 && sumMonth !=0) then print the average it works but for the first 2 years and it's blank for the last year

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
So anybody has any suggestion? cause tried a few other ways and I'm still stuck.

Carey Brown
Saloon Keeper
Posts: 9831
80
• 1
• Number of slices to send:
Optional 'thank-you' note:
Are you keeping track of the count of values being added to sum?
Average = sum / count.

Marshal
Posts: 8612
623
• 1
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:verything is good except for the fact that I don't want my average to be calculated line by line for every single of those sums above. I want to divide that 42 by the number of iterate to that point which 7 and print it out only once

Hi, so, that number 42 you get after some amount of iterations (after 7). Who defines the number of iterations?

saeid jamali wrote:for(int i=0; i<object.size(); i++ )

This time was 7 iterations, next time there could be 10. Of course you're not hardcoding that number, so where do you get that number when it comes down to division?
42 / ?

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown wrote:Are you keeping track of the count of values being added to sum?
Average = sum / count.

How can I keep track?

Carey Brown
Saloon Keeper
Posts: 9831
80
• 1
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:

Carey Brown wrote:Are you keeping track of the count of values being added to sum?
Average = sum / count.

How can I keep track?

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown wrote:

saeid jamali wrote:

Carey Brown wrote:Are you keeping track of the count of values being added to sum?
Average = sum / count.

How can I keep track?

Yes I have done this but the for some reason I get the average for the first two years with no problem and then for the last year the result for average is totally blank.
to avoid exception can not divide by zero I added an if statement below my main if statement

Carey Brown
Saloon Keeper
Posts: 9831
80
• 1
• Number of slices to send:
Optional 'thank-you' note:
I can't see the problem at this point. Perhaps you could repost the whole code instead of snippits.

Carey Brown
Saloon Keeper
Posts: 9831
80
• 1
• Number of slices to send:
Optional 'thank-you' note:
After the loop completes, are you checking to see if count != 0?

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

So basically when ever the year changes average will be equal to sum/count and it checks to see count is not equal 0So it doesn't calculate it for the firs loop

Carey Brown
Saloon Keeper
Posts: 9831
80
• 1
• Number of slices to send:
Optional 'thank-you' note:
Seems like you are ALMOST THERE.
A suggestion: If you want people to help you post a self contained program that is compilable and has the proper indentation. Here is your code with some cleanup. Isn't this much better? Would be even better with some hard coded dummy data. Check out the comments I added.

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
Thank you. I was missing a if(count !=0) at the end

Carey Brown
Saloon Keeper
Posts: 9831
80
• Number of slices to send:
Optional 'thank-you' note:
You're welcome.