11 Replies Latest reply: Apr 3, 2012 4:43 PM by morgalr

# Magic Square Problems

I'm trying to create a Magic Square for a class project, and I'm completely lost(lucky to be as far as I am). Can someone please take a look at my code and tell me what I'm doing wrong? Thanks very much for any help you can provide.
``````public class MagicSquare {

public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
if (N % 2 == 0) throw new RuntimeException("N must be odd");

int[][] magic = new int[N][N];

int row = N-1;
int col = N/2;
magic[row][col] = 1;

for (int i = 2; i <= N*N; i++) {
if (magic[(row + 1) % N][(col + 1) % N] == 0) {
row = (row + 1) % N;
col = (col + 1) % N;
}
else {
row = (row - 1 + N) % N;
// don't change col
}
magic[row][col] = i;
}

// print results
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (magic[i][j] < 10)  System.out.print(" ");  // for alignment
if (magic[i][j] < 100) System.out.print(" ");  // for alignment
System.out.print(magic[i][j] + " ");
}
System.out.println();
}

}
}``````
Here's the error I receive when trying to run the program:
``````Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MagicSquare.main(MagicSquare.java:4)``````
Edited by: EJP on 4/04/2012 09:31: added { code } tags. Please use them/
• ###### 1. Re: Magic Square Problems
Look at the line number:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MagicSquare.main(MagicSquare.java:4)
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
You do not have a 0th arg ie you have called your app without an argument
• ###### 2. Re: Magic Square Problems
Thank you. I know this is a completely stupid question(I'm in way over my head on this project), but how can I create the argument for my app?

Edited by: 915786 on Mar 22, 2012 7:42 PM
• ###### 3. Re: Magic Square Problems
``java MagicSquare 5``
The 5 is args[0]. Just to illustrate:
``java MagicSquare 5 monkey 12.5``
args[0] = "5"
args[1] = "monkey"
args[2] = "12.5"
• ###### 4. Re: Magic Square Problems
I think I got it working thanks to your advice (other than a couple small issues), but is there a way that I can print an error message(maybe in the while loop) if the user inputs a negative/even/>21 number? I've been messing around with it for a while, and whatever I do it doesn't print anything and just keeps asking me to enter a new number(when a neg/even/>21 number is input). Thank you! Here's my new code:
``````import java.util.Scanner;

public class MagicSquare{
public static void main (String args[]){

Scanner input = new Scanner(System.in);
int N;

while (true)
{
System.out.println("Please input the size of the Magic Square:");
N = input.nextInt();
if (N%2==1&&N<=21)
{
break;
}
}
int [][]magic = new int[N][N];
int row = N -1;
int col = N/2;
magic[row][col] = 1;

for (int i = 2; i <= N*N; i++){
if (magic[(row + 1) % N][(col + 1) % N] == 0){
row = (row + 1) % N;
col = (col + 1) % N;
}
else
{
row = (row - 1 + N) % N;
}
magic [row][col] = i;
}
for(int i = 0; i < N; i++){
System.out.println("  ----------------------");
for(int j = 0; j < N; j++)
{
if (magic[i][j] < 10)
System.out.print(" | ");
System.out.printf("%4d",magic[i][j]);
}
System.out.print("\n");
}
System.out.print("  ----------------------");
}
}``````
Edited by: 915786 on Mar 23, 2012 4:23 PM

Edited by: EJP on 4/04/2012 09:31: added { code } tags: please use them; and removed the boldface: please don' use that.
• ###### 5. Re: Magic Square Problems
I'm pretty sure you can figure this out on your own. Empower yourself! Really really try and if you can't get it in a day or two come back and we'll talk again
• ###### 6. Re: Magic Square Problems
I'd strongly recommend you invest a few hours in learning to use a debugger. If you stay in the computer field, you'll have to do this eventually anyway. The earlier you learn to use one, the easier your life will be!
• ###### 7. Re: Magic Square Problems
aksarben wrote:
I'd strongly recommend you invest a few hours in learning to use a debugger. If you stay in the computer field, you'll have to do this eventually anyway. The earlier you learn to use one, the easier your life will be!
Amen! And bothers can I get a halleluya too!
• ###### 8. Re: Magic Square Problems
oh, bother...
• ###### 9. Re: Magic Square Problems
Just a bit of tongue in cheek agreement there--the debugger is the most over looked tool available to a programmer, and in the beginning it can be one of the best learning tools, yet it is almost never introduced in any programming class.

If you've read the forums for any length of time, you've probably seen my comments along the line of: "Stop what you are doing now, do not program another line until you learn how to use your debugger. It is that important."
• ###### 10. Re: Magic Square Problems
Which debugger would you recommend I use?
• ###### 11. Re: Magic Square Problems
Which ever one comes with your IDE.