"Il y a peu de choses qui me soient impossibles..."
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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?
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.htmlsaeid 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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
What does that do? Shouldn't it readsaeid jamali 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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
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
saeid jamali wrote:for(int i=0; i<object.size(); i++ )
Carey Brown wrote:Are you keeping track of the count of values being added to sum?
Average = sum / count.
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?
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?