title: how to create magic square in java?
body: <p>my task is to write a program that ask user for an input and the method will return whether or not the input forms a magic square. No matter what I enter into the console, the program returns that I entered a magic square. What am I missing?</p>
  
  <p>Magic square definition: a 2 dimensional array is a magic square if the sum of the rows, columns, and diagonals is the same.</p>
  
  <pre><code>here is the Code:
  
  
      public class MagicSquares {
  
  
  public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      ArrayList&lt;Integer&gt; ints = new ArrayList&lt;Integer&gt;();
      int current = 0;
      do{
          System.out.print("Enter an int. Enter -1 when done&gt;");
          current = Integer.parseInt(in.nextLine());
      }while(current != -1);
      int numInputs = ints.size();
      int square = (int) Math.sqrt(numInputs);
  
      if(square*square == numInputs){
          int[][] intSquare = new int[square][square];
          int x = 0;
          while(x &lt; numInputs){
              for(int y = 0; y &lt; square; ++y){
                  for(int z = 0; z &lt; square; ++z){
                      intSquare[y][z] = ints.get(x);
                      ++x;
                  }
              }
          }
          if(isMagicSquare(intSquare)){
              System.out.println("You entered a magic square");
          }else{
              System.out.println("You did not enter a magic square");
          }
  
      }else{
          System.out.println("You did not enter a magic square. " +
                  "You did not even enter a square...");
      }
  }
  
  
  private static Boolean isMagicSquare(int[][] array){
      int side = array.length;
      int magicNum = 0;
      for(int x = 0; x &lt; side; ++x){
          magicNum =+ array[0][x];
      }
      int sumX = 0;
      int sumY = 0;
      int sumD = 0;
      for(int x = 0; x &gt; side; ++x){
          for (int y = 0; y &lt; side; ++y){
              sumX =+ array[x][y];
              sumY =+ array[y][x];
          }
          sumD =+ array[x][x];
          if(sumX != magicNum  || sumY != magicNum || sumD != magicNum){
              return false;
          }
      }
      return true;
  }
  
  
  
  }
  </code></pre>