Strange output from an array

I'm getting some really funky output from a program that should print out a list of numbers, but instead I get this output:

Sum of rows: [I@3e25a5

Sum of columns: [I@19821f

Sumof diagonals: [I@addbf1

The array is a magic square

Sum of rows: [I@42e816

Sum of columns: [I@9304b1

Sumof diagonals: [I@190d11

The array is a magic square

Sum of rows: [I@a90653

Sum of columns: [I@de6ced

Sumof diagonals: [I@c17164

The array is a magic square

Here's my code:

publicclass MagicSquare

{

publicstaticvoid isMagic (int[][] b )

{

int numRow = b[0].length;

int numCol = b[1].length;

int[] rowSum =newint[b[0].length];

int[] colSum =newint[b[1].length];

int[] diaSum =newint[2];

for (int i = 0; i < numRow; i++ )

for (int j = 0; j < numCol; j++ )

{

int x = 0;

rowSum[x] = b[i][j];

colSum[x] = b[j][i];

if (i == j)

{

diaSum[x] = b[i][j];

}

x++;

}

System.out.println("Sum of rows: " + rowSum);

System.out.println("Sum of columns: " + colSum);

System.out.println("Sumof diagonals: " + diaSum);

if(true)

{

System.out.println("The array is a magic square");

}

else System.out.println("The array is not a magic square");

}

publicstaticvoid main(String[] args)

{

int[][] xArray ={{5, 9, 1},{3, 4, 8},{7, 2, 6}};

int[][] yArray ={{1, 3, 16, 14},{8, 15, 2, 9},

{13, 6, 11, 4},{12, 10, 5, 7}};

int[][] zArray ={{18, 24, 5, 6, 12},{10, 11, 17, 23, 4},

{22, 3, 9, 15, 16},{14, 20, 21, 2, 8},

{1, 7, 13, 19, 25}};

isMagic(xArray);

isMagic(yArray);

isMagic(zArray);

}

}

The if statement is set to true for debugging, but what I don't understand is why I'm getting that weird output, according to Eclipse I don't have any errors, and as far as I can tell, there shouldn't be anything wrong with it.

[4341 byte] By [Kronoa] at [2007-11-27 11:18:09]
# 1

That's because an int[] does not override the toString() method.

Try it like this:

// ...

System.out.println("Sum of rows: " + java.util.Arrays.toString(rowSum));

// ...

which will print the elements from an int[] instead.

prometheuzza at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks, that worked. Now the only problem I have is that the x variable in the for loop isn't incrementing at all.

Kronoa at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 3

well, when you set x = 0 within the for loop, it's going to do what you tell it to do.... == 0.

but even fixing this, your logic is messed up. x is not doing what you think it is doing and so your row and column sums are being messed up. You need to think through what you are doing on paper before committing it to code. I would scrap this and retry, but plan ahead on paper.

Message was edited by:

petes1234

petes1234a at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 4

do you want to want to print that the matrix is magic square or not along with the sum of row elements,column elements and diagonal elements

student-javaa at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 5

but even fixing this, your logic is messed up. x is not doing what you think it is doing and so your row and column sums are being messed up. You need to think through what you are doing on paper before committing it to code. I would scrap this and retry, but plan ahead on paper.

Well then, what should I do? I am at a loss. :(

Message was edited by:

Krono

Kronoa at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 6

> Well then, what should I do? I am at a loss. :(

1) Don't throw up your hands and give up. That won't solve anything.

2) Don't expect us to just give you code. That would be cheating (unless a Duke's prostitute happens by).

3) Think through your logic step by step on paper. And repeat those steps in your program.

4) If you want the size of rows in your array and columns getting array[0].length and array[1].length isn't the way to do it. Fortunately you have square arrays, so this simplifies what you are doing, but if you ever deal with rectangular arrays, this will fail miserably. You'd have to use array.length and array[0].length.

petes1234a at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 7

Why would I not do array[0].length and array[1].length? that does correspond to number of rows and columns, right?

Can you give me a clue as to how I should go about doing this? I had originally intended to do this without using arrays, but I couldn't find a way to do that. And unfortunately, googleing the problem I have doesn't turn up with a useful section of the Java tutorial that explains how what I'm trying to do works.

Kronoa at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 8

> Why would I not do array[0].length and

> array[1].length? that does correspond to number of

> rows and columns, right?

No.

int[][] arr = new int[M][N]

int numRows = arr.length; // M rows

int numCols = arr[0].length; // N columns

int numColsAgain = arr[1].length; // same as above

Of course, you could choose to view the rows and columns as switched.

jverda at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...
# 9

> No.

> int[][] arr = new int[M][N]

>

> int numRows = arr.length; // M rows

> int numCols = arr[0].length; // N columns

> int numColsAgain = arr[1].length; // same as above

yup, just as I said above.

petes1234a at 2007-7-29 14:29:13 > top of Java-index,Java Essentials,Java Programming...