Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

karina val
Greenhorn
Posts: 6
I have 3 arrays that were filled from a file. The first column is breakfast, the second column is lunch, and the third column is dinner. But now I have to get the totals for each day (or row), and I don't know how to do that without having a multi dimensional array since I already have 3 arrays filled.
The file looks like this, the numbers correspond to calories, each row is a day:
200 1000 800
450 845 1200
800 250 400
0 1500 1800
600 500 1000
700 1400 1700
675 400 900

I'm not asking for a code, just a suggestion or idea how to get around this :S I know how to add rows and columns in 2d arrays but this threw me off.

karina val
Greenhorn
Posts: 6
So I got the sum for the first row using this... haha

int first = 0;
int sum = 0;

sum += breakfast[first] + lunch[first] + dinner[first];
System.out.println(sum);

but how could I get so it goes through the rest of the indexes without retyping or copying and pasting the code 7 times...?

Rob Spoor
Sheriff
Posts: 20531
54
Use a for-loop.

Patrick Noah
Greenhorn
Posts: 28
Calculate a daily total for say, day 0. iterate through the days and have the calculation for the daily total in the loop.

karina val
Greenhorn
Posts: 6
I got it! Thanks

Rob Spoor
Sheriff
Posts: 20531
54
Why don't you replace day with your loop variable i?

Oh, and welcome to the Ranch!

Campbell Ritchie
Sheriff
Posts: 48968
60
karina val wrote: . . .
int sum = 0;

sum += breakfast[first] + lunch[first] + dinner[first]; . . .
Like many beginners, you have some unnecessary code. You mean int sum = breakfast[first] + lunch[first] + tea[first];

Campbell Ritchie
Sheriff
Posts: 48968
60
Rob Spoor wrote:Why don't you replace day with your loop variable i? . . .
As I said earlier, you have some unnecessary lines.

 It is sorta covered in the JavaRanch Style Guide.