aspose file tools*
The moose likes Beginning Java and the fly likes Comparing the sums of rows and columns in a 2D array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing the sums of rows and columns in a 2D array" Watch "Comparing the sums of rows and columns in a 2D array" New topic
Author

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

Lonnie Wood
Greenhorn

Joined: Feb 16, 2011
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
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Lonnie Wood
Greenhorn

Joined: Feb 16, 2011
Posts: 15

Henry,

I had tried about twenty different comparison strings but nothing worked.

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
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

Lonnie Wood wrote:
I had tried about twenty different comparison strings but nothing worked.

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

Joined: Feb 16, 2011
Posts: 15

Henry,

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

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing the sums of rows and columns in a 2D array