Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Not doing what i want

Pablo Varela Rojas
Greenhorn
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
Posts: 3
LOL i found out my error suma = 0; in the third for sorry for posting this.

Anayonkar Shivalkar
Bartender
Posts: 1557
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.