Results 1 to 7 of 7
  1. #1
    jae04cc is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Unhappy Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    I'm in a programming class, and we are supposed to be making a magic square with size N, inputted by the user. I also have to use at least two methods, including squareBuilder, and squarePrinter. The code below is what i have so far, and when i run it, runs fine, asks for input, loop at the beginning ensures a valid input is received, but then when a valid input is entered, i get this error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at MagicSquare.squareBuilder(MagicSquare.java:16)
    at MagicSquare.main(MagicSquare.java:46)

    When I started working with it, i had it working without methods, all in the main method. When i put the square building code and the square printing code from the main class into methods and then tried calling them is when i ran into this problem.

    Java Code:
    import java.util.Scanner;
    
    public class MagicSquare {
    	
    	 //initializes N and magic
    	static int N = 0;
    	static int[][] magic = new int[N][N];
    	
    	//square builder method
    	public static int squareBuilder(){
            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;}
    		return magic [col][row];}
    
    	//square printer method
        public static void squarePrinter(){
    	for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
    			if (magic[i][j] < 10)  System.out.print(" ");
                if (magic[i][j] < 100) System.out.print(" ");
                System.out.print(magic[i][j] + " ");}
            System.out.println();}}
    	
    	//main method 
        public static void main(String[] args) { 
    	     	//user prompt and assigns user input to N
    		 System.out.println("What size magic square would you like to create?");   
    	     Scanner input = new Scanner(System.in);
    			N = input.nextInt();
    	        
    			//checks to make sure the number is positive and odd
    			while (N % 2 == 0) {
    	        	System.out.println("N can't be negative or even, try again.");
    	        	Scanner input1 = new Scanner(System.in);
    	    		N = input1.nextInt();
    	        }
    			
    			squareBuilder();
    			squarePrinter();
    
    	    }
    
    }
    any help would be greatly appreciated! and I'm new to java so please be nice if i made a stupid error :)

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    That is because of how you created your magic array. Look at the value of N that you used. Arrays have fixed size. You can't use a variable and expect the array to dynamically change with the value of the variable.

  3. #3
    jae04cc is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    so would i have to initialize the array after the scanner?...and if so, how do i use it in the methods?

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    That's exactly what you do. Just define magic and don't initialize it, maybe comment that it gets initialized after you've obtained the input from the user. You are not referencing magic until after you've obtained the user input anyway, so there is no harm. Once you have N, you can initialize it just like you did, but do it on line 37.

    Java Code:
    magic = new int[N][N];

  5. #5
    jae04cc is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    Java Code:
    public class MagicSquare {
    	
    	 //initializes N and magic
    	static int N = 0;
    	static int[][] magic;
    	
    	//square builder method
    	public static int squareBuilder(){
            int row = N-1;
            int col = N/2;
             magic = new int[N][N];
            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;}
    		return magic [col][row];}
    is that what you meant?...i initialized it in the squareBuilder method actually instead of in the main method..is that ok?..the code runs, but is that not the proper way to do it?

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    There's nothing wrong with doing it that way at all.

  7. #7
    jae04cc is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    Awesome thanks a bunch!

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2012, 10:13 PM
  2. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  3. Replies: 5
    Last Post: 04-05-2010, 02:28 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •