Results 1 to 7 of 7
- 04-26-2012, 09:45 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
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.
any help would be greatly appreciated! and I'm new to java so please be nice if i made a stupid error :)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(); } }
- 04-26-2012, 09:46 PM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-26-2012, 09:53 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
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?
- 04-26-2012, 10:11 PM #4
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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];
- 04-26-2012, 10:53 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
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?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];}
- 04-27-2012, 12:33 AM #6
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
There's nothing wrong with doing it that way at all.
- 04-27-2012, 12:37 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
By stephanie904 in forum New To JavaReplies: 4Last Post: 04-23-2012, 10:13 PM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By paul in forum New To JavaReplies: 8Last Post: 03-05-2011, 03:53 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
By dark_metal in forum New To JavaReplies: 5Last Post: 04-05-2010, 02:28 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks