I'm working on a program that asks the user for an integer that will be used as the order for a Magic Square. I've set up the constructor, the addVal, the removeVal, and the Solve, methods. I think the problem is now somewhere in my isValid method. When I run the program it returns a not so magic square with the integers appearing in the square 1 through N*N. I'm completely at a loss at this point on what the problem is. I'm almost certain it's the isValid method, but I don't see what's causing the problem when it tries to add up the values. Any help would be appreciated, here's what I have so far:

import java.util.*; public class MagicSquare { // the current contents of the cells of the puzzle values[r][c] // gives the value in the cell at row r, column c private int[][] values; private int order; // the order (i.e., the dimension) of the puzzle private boolean [] available; // numbers still available to be added to the magic square private int totalSquares; // total number of squares on the board private int sum; // sum that the rows and columns must add up to public MagicSquare(int order) { values = new int[order][order]; this.order = order; totalSquares = order * order; sum = (order * order * order + order)/2; for (int i = 0; i < order; i++) { for (int j = 0; j < order; j++) { values[i][j] = 0; } } available = new boolean[totalSquares]; for (int i = 0; i < totalSquares; i++) { available[i] = true; } } public void removeVal(int i, int row, int col) { values[row][col] = 0; available[i] = true; } public boolean addVal(int row, int col) { System.out.println(row + "" + col); if (row == order - 1 && col == order) { display(); return true; } for(int i = 1; i <= totalSquares; i++) { if(isValid(i, row, col)) { values[row][col] = i; available[i] = false; int newCol, newRow; if (col == order - 1) newCol = 0; else newCol = col + 1; if (newCol == 0) newRow = row+1; else newRow = row; if (addVal(newRow, newCol)) return true; else removeVal(i, row, col); } } return false; } public boolean isValid(int i, int row, int col) { if (available[i] == false) return false; int colCount = 0; int rowCount = 0; int colSum = 0; int rowSum = 0; for( int j = 0; j < order; j ++) { System.out.println("col is " + col); System.out.println("j is " + j); System.out.println("row is " + row); if (values[j][col] == 0) colCount ++; if (values[row][j] == 0) rowCount ++; rowSum += values[row][j]; colSum += values[j][col]; display(); } System.out.println("colSum is " + colSum); System.out.println("rowSum is " + rowSum); if (colCount == 1) { if (i + colSum == sum && i + rowSum == sum) return true; } if (rowCount == 1) { if (i + colSum == sum && i + rowSum == sum) return true; } else if (i + colSum > sum || i + rowSum > sum) return false; return true; } public boolean solve() { if (addVal(0,0)) return true; else return false; } public void display() { for (int r = 0; r < order; r++) { printRowSeparator(); for (int c = 0; c < order; c++) { System.out.print("|"); if (values[r][c] == 0) System.out.print(" "); else { if (values[r][c] < 10) { System.out.print(" "); } System.out.print(" " + values[r][c] + " "); } } System.out.println("|"); } printRowSeparator(); } private void printRowSeparator() { for (int i = 0; i < order; i++) System.out.print("-----"); System.out.println("-"); } public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("What order Magic Square would you like to solve? "); int order = console.nextInt(); MagicSquare puzzle = new MagicSquare(order); if (puzzle.solve()) { System.out.println("Here's the solution:"); puzzle.display(); } else { System.out.println("No solution found."); } } }

As I said, I think the problem is with my isValid method, but I'm not positive. I'm afraid to change anything else right now because as far as I can tell the other methods are ok. Thanks for any help!