Search code examples
javaarraysmethodscompiler-errorsmagic-square

Java Magic Square - Sum Column and Sum Row Errors


I've been assigned a school assignment to make and check for a 'Magic Square' in a 2d array generated by the user in a N*N matrix.

So far, I have gotten most of the code right (I have tested each method individually). However, I am unable to correct two final errors that keep popping up within my 'sumColumn' and 'sumRow' methods. Here is my code for the two aforementioned methods:

public static int sumColumn(int[][] square, int columnNumber)
{
    int sum = 0 ;
    for (int j = 0; j < square.length ; j++)
    {
        for (int i = 0; i < square.length; i++)
        {
            sum = sum + square[i][j] ;
        }
    }                        
    return sum ;
}

public static int sumRow(int[][] square, int rowNumber)
{
    int sum = 0 ;
    for (int i = 0; i < square.length; i++)
    {
        for (int j = 0; j < square.length; j++)
        {
            sum = sum + square[i][j] ;
        }
    }
    return sum ;
}

And here is the output along with the error that appears upon calling from the main method:

Please enter a value for N:
1
Please enter 1 numbers: 
1
This is the square you input:
+-+
|1|
+-+
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method sumRow(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)
The method sumColumn(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.main(MagicSquares.java:167)

Some fiddling with 'sumRow' and 'sumColumn' will instead yield another error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
rowNumber cannot be resolved to a variable at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)
colNumber cannot be resolved to a variable at squares.MagicSquares.main(MagicSquares.java:167)

Any help would be much appreciated in resolving this issue! Thanks!

PS: I've only started programming last month, so be kind :3

Edit: here is the method which checks if each row, column and main and secondary diagonals are equal to make a Magic Square.

boolean status = true ;
    int sum = sumDiagonal(square) ;
    if (sumSecondaryDiagonal(square) != sum)
    {
        status = false ;
    }
    else
    {
        for (int row = 0; (row < square.length) && status; row ++)
        {
            if (sum != sumRow(square, square.length))
            {
                status = false ;
            }
        }
        for (int col = 0; (col < square.length) && status; col ++)
        {
            if (sum != sumColumn(square, square.length))
            {
                status = false ;
            }
        } 
    }
    return status;

Solution

  • Your sumColumn method expects two arguments:

    1. int[][] square
    2. int columnNumber

    In your main method, it seems you are just providing a single argument, the int[][], and you forgot to include the columnNumber as a second argument.

    Same applies to the sumRow method.

    I made the above findings based on the error message you posted:

    The method sumRow(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)

    It says that, in your MagicSquares.java file, line number 105, you invoke sumRow method. It further indicates that (The method sumRow(int[][], int) in the type MagicSquares), the method you implemented, can not be used/is not applicable for a call with arguments (int[][]), at line 105.