File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding arrays?

 
karina val
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a for-loop.
 
Patrick Noah
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it! Thanks


 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you replace day with your loop variable i?

Oh, and welcome to the Ranch!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Why don't you replace day with your loop variable i? . . .
As I said earlier, you have some unnecessary lines.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic