Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Comparing the sums of rows and columns in a 2D array

Lonnie Wood
Greenhorn
Posts: 15
Here's the code I have for summing the totals of my columns and my rows.

I'm trying to figure out how to say if row one does not equal row two. I'm just not getting the syntax here.

If anyone could help I'd appreciate it.

Thanks

Henry Wong
author
Marshal
Posts: 21115
78
Lonnie Wood wrote:
I'm trying to figure out how to say if row one does not equal row two. I'm just not getting the syntax here.

Well, you are not saving the row totals (or the column totals) anywhere. You calculate a row total, print it, and then calculate the next row using the same variables.

You can't compare one row total to another because your program never have two row totals (at the same time) to allow for any comparison -- much less, for a specific comparison.

Henry

Lonnie Wood
Greenhorn
Posts: 15
Henry,

I'm calculating the totals, but how do I store the value is a better question I guess. How can I say that columnTotal for column0 = columnTotal0 and columnTotal for column1 = columnTotal1?

Hence the reference to a syntax problem.

Thanks

Henry Wong
author
Marshal
Posts: 21115
78
Lonnie Wood wrote:

I'm calculating the totals, but how do I store the value is a better question I guess. How can I say that columnTotal for column0 = columnTotal0 and columnTotal for column1 = columnTotal1?

Have you tried creating an array for the column totals? Then you can say "columnTotals[col]" as the total for a particular column.

Henry

Lonnie Wood
Greenhorn
Posts: 15
Henry,

No I haven't but that makes sense. Let me try that out and let you know.

Thanks

Campbell Ritchie
Sheriff
Posts: 48917
58
I take it you have a "rectangular" array, ie all the rows are the same length. Otherwise, if a row is longer than the first row, you will omit its last element and get a wrong answer. If a row is shorter, you will get the far less serious result of an Exception.