0

This is probably a very silly question but I am trying to print this output so that it only prints out what is in the system.out.println's not true/false. So in other words I want to get rid of the true/false.

I know there are other issues such as my if (magic == false) method but I just want to focus on getting rid of the true/false.

public boolean magic() {
  boolean diagMagic;
  diagMagic = sumOtherDiag() == sumMainDiag();
  int[] colArray = new int[square.length];
  int[] rowArray = new int[square.length];
  for (int k = 0; k < square.length; k++) {
     rowArray[k] = sumRow(k);
  }
  for (int i = 0; i < square.length; i++) {
     colArray[i] = sumCol(i);
  }
  boolean ifMagic = rowArray.equals(colArray);
  boolean magic = ifMagic == diagMagic;

  if(magic == false) {
     System.out.println("This is a Magic Square");
  } else
      System.out.println("This is NOT a Magic Square");
  return diagMagic;
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Kiki
  • 1
  • 1
    I don't understand what you're asking. The method should focus on evaluating if the *magic* is true or false. The client/consumer of this method must evaluate the result of the method and provide a proper message for user. `System.out.println` should not be used in `magic` method. – Luiggi Mendoza Feb 09 '15 at 15:52
  • share your sumCol, sumRow nethod. Juts remove sysout from one of those two method. – SMA Feb 09 '15 at 15:53
  • Very confused question. – Francesco Palmiotto Feb 09 '15 at 15:54
  • The only output is one of these strings ("This is...Square"). If you see anything else it's printed elsewhere. – laune Feb 09 '15 at 15:54
  • You actually want to return a String instead of a boolean? Btw, `if(magic == false)` is useless. Try `if(!magic)` instead – Arnaud Denoyelle Feb 09 '15 at 15:54
  • @ArnaudDenoyelle I would recommend returning a `boolean` or an `enum` rather than a `String` that contains the message to show to the user. Let the view layer takes care of these messages, not the internal API. – Luiggi Mendoza Feb 09 '15 at 15:55
  • @LuiggiMendoza Totally agree with that. But I do not understand what the OP wants. – Arnaud Denoyelle Feb 09 '15 at 15:55
  • Comparing two arrays the way you need it can't be done using equals. Also, if correct comparison of the row and column sums returns that they are equal it doesn't mean that the square is magic. They must all have the same value, same as the diagonals. – laune Feb 09 '15 at 15:58
  • 1
    Computing `magic = ifMagic == diagMagic;` will suddenly make the square magic if neither ifMagic nor diagMagic is true (both are false) and therefore they are equal, so magic becomes true... – laune Feb 09 '15 at 16:00

0 Answers0