permaculture playing cards*
The moose likes Beginning Java and the fly likes Not doing what i want Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not doing what i want" Watch "Not doing what i want" New topic
Author

Not doing what i want

Pablo Varela Rojas
Greenhorn

Joined: Mar 05, 2013
Posts: 3
Hello as you can see im a begginer and well i need help in my code if is not that of a bother. Well as you can the array gets filled up with the loops but i want it to sum up each row and column but it doesnt
and i dont know how to make it do that please i need some help i keep on searching about 2 dimensional arrays on java , i know that its just an array of arrays anyways thank you in advance and sorry for
my english its not my native language tnx!

Pablo Varela Rojas
Greenhorn

Joined: Mar 05, 2013
Posts: 3
LOL i found out my error suma = 0; in the third for sorry for posting this.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Hi Pablo,

Welcome to CodeRanch!

Its nice to see you are using code tags in your first posts

Currently, it seems that you are adding all the elements of array of arrays (vacas).

Now, if I understand correctly, you want to:
1) Get sum of each row
2) Get sum of each column

First part is quite straightforward.
Since vacas is array of arrays:
1) Iterate over vacas. Each element of vacas (i.e. vacas[i]) itself is an array.
2) Iterate over that array (i.e. array at vacas[i]) and sum up all the elements there.

For part two, you need to consider that since all arrays are not of same length (you are taking array length from user), it might be tricky.
To make this easy, you need to check if the index you are accessing is greater than array length.

e.g. consider below value for vacas:
c/r c1 c2 c3 c4 c5
r1 11 21 31
r2 21 22 32 42 52
r3 31 32

Now, when you go for sum of column 3, make sure that you should not access element at 3rd row and 3rd column, otherwise, you'll get ArrayIndexOutOfBoundsException. To avoid this, firstly, please check that if length of r3 is less than index of column you want to sum up.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not doing what i want
 
Similar Threads
int [] [] ??
How to enter whole row of elements into 2d array
Base Converter
How to input user values multi-dimensional arrays
Linear comparison algorithm