Java – Magic Square Code (java)

arraysjavamagic-square

I've been having an issue with my Magic Square code. It continually prints "This is a magic square" even when I'm positive it isn't.
You enter 16 integers, and then the code is supposed to run and determine whether the entered integers create a magic square (i.e. the sum of all rows, columns, and diagonals are all equal).

I can't figure out how to make it print false.

import java.util.*;
public class MagicSquare {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner keyboard = new Scanner (System.in);

        int [] [] square = new int [4][4];
        for (int row = 0; row < 4; row++)
        {
            for (int col = 0; col < 4; col++)
            {
                System.out.println("Input value for row " + (row+1) + " column " + (col+1));
                square[row][col] = keyboard.nextInt();
            }
        }

        int [] sumRow = new int [4];
        int [] sumCol = new int [4];

        int sum = 0;

        for (int row = 0; row < 4; row ++)
        {
            for (int col = 0; col < 4; col ++)
            {
                sum = sum + square[row][col];
                sumRow[row] = sum;
            }
            System.out.println("sum row " + row + "\n" + sumRow[row]);
            sum = 0;
        }

        sum = 0;

        for (int col = 0; col < 4; col ++)
        {
            for (int row = 0; row < 4; row ++)
            {
                sum = sum + square[row][col];
                sumCol[col] = sum;
            }
            System.out.println("sum columns " + col + "\n" + sumCol[col]);
            sum = 0;
        }

        int [] sumDiag = new int [4];
        sum = 0;

        for (int row = 0; row < 4; row++)
        {
            for (int col = 3; col > -1; col--)
            {
                sum = sum + square [row][col];
                sumDiag[row] = sum;
            }
            System.out.println("sum diagonal " + row + "\n" + sumDiag[row]);
            sum = 0;
        }

        int [] sumDiag2 = new int [4];
        sum = 0; 

        for (int col = 0; col < 4; col ++)
        {
            for (int row = 3; row > -1; row --)
            {
                sum = sum + square[row][col];
                sumDiag2[col] = sum;
            }
            System.out.println("sum diagonal 2 " + col + "\n" + sumDiag2[col]);
        }

        boolean bool = false;

        int k = 0; int j = 1; 
        do
        {
            if (sumRow[k] == sumRow[j])
            {
                k = j;
                j += 1;
                bool = true;
            }
            else
            {
                bool = false;
                System.out.println("Not a magic square");
                break;
            }
        } while ((k < 4) && (j >- 1));

        k = 0; j = 1;
        do
        {
            if (sumCol[k] == sumRow[j])
            {
                k = j;
                j += 1;
                bool = true;
            }
            else 
            {
                bool = false;
                System.out.println("Not a magic square");
                break;
            }
        } while ((k < 4) && (j >= -1));



        String TorF = "";
        if (bool = true)
        {
            TorF = "is";
        }
        else if (bool = false)
        {
            TorF = "is not";
        }

        System.out.println("This " + TorF + " a magic square.");

    }

}

Best Solution

There is a number of issues in your code, as said in the comments.

First, you only need to verify the main and secondary diagonals.

Second, your code to compare the sums doesn't work for all cases and doesn't compare the diagonals.

Also, the if in the end is just wrong. Do this instead:

if (bool) {
} else {
}

And here is a solution:

int order = square.length;

int[] sumRow = new int[order];
int[] sumCol = new int[order];
int[] sumDiag = new int[2];

Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);
Arrays.fill(sumDiag, 0);

for (int row = 0; row < order; row++) {
  for (int col = 0; col < order; col ++) {
    sumRow[row] += square[row][col];
  }
  System.out.println("sum row " + row + "\n" + sumRow[row]);
}

for (int col = 0; col < order; col++) {
  for (int row = 0; row < order; row ++) {
    sumCol[col] += square[row][col];
  }
  System.out.println("sum columns " + col + "\n" + sumCol[col]);
}

for (int row = 0; row < order; row++) {
  sumDiag[0] += square[row][row];
}
System.out.println("sum diagonal 0 " + "\n" + sumDiag[0]);

for(int row = 0; row < order; row++) {
  sumDiag[1] += square[row][order - 1 - row];
}
System.out.println("sum diagonal 1 " + "\n" + sumDiag[1]);

boolean bool = true;

int sum = sumRow[0];
for (int i = 1; i < order; i++) {
  bool = bool && (sum == sumRow[i]);
}
for (int i = 0; i < order; i++) {
  bool = bool && (sum == sumCol[i]);
}
for (int i = 0; i < 2; i++) {
  bool = bool && (sum == sumDiag[i]);
}

String tOrF = "";
if (bool) {
  tOrF = "is";
} else {
  tOrF = "is not";
}
System.out.println("This " + tOrF + " a magic square.");

Furthermore, there are some things you could do to optimize this code.