Results 1 to 5 of 5
- 01-04-2012, 03:36 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
When I run my program below, I get correct results but at the end of the output an error appears saying:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException… 30
at Pro.executeCommands(Pro.java:194)
at Pro.main(Pro.java:108)
Based from the error message, there's something wrong with line 194 and 108. Line 108 of the code simply calls the method executeCommands();
Line 194 (found inside method executeCommands) contains this: command = commandArray[ ++commandNum ][ 0 ];
The "30" in the error is possibly the constant I declared at the start of the class which has the value of 30. So the error may be because of my inclusion of maxCommands in some statements. IDK
So here's the code snippet so you can figure out (I deleted a couple of methods, some are called inside the main method, which I know has nothing to do with the error) :
-> Any suggestions on how I can solve this? Any form of help always appreciated. Thanks in advance.Java Code:import java.util.Scanner; public class Pro { static final int maxCommands = 30; static final int size = 20; static int floor[][]; static int commandArray[][]; static int command, distance, direction, count, xPos, yPos; static boolean penDown; static String prompt; public static void main ( String args[] ) { int inputCommand = 0; int spaces; Scanner in = new Scanner ( System.in ); initialize(); displayMenu(); System.out.println ( prompt ); while ( inputCommand != 9 ) { inputCommand = in.nextInt(); // execute commands if command is 9 if ( inputCommand == 9 || count == maxCommands ) executeCommands(); else { if ( count < maxCommands ) { commandArray[ count ] [ 0 ] = inputCommand; if ( inputCommand == 5 ) { spaces = in.nextInt(); commandArray[ count ][ 1 ] = spaces; } } } count++; } } public static void initialize () { direction = 0; count = 0; xPos = 0; yPos = 0; penDown = false; floor = new int[ size ][ size ]; commandArray = new int[ maxCommands ][ 2 ]; for (int i = 0; i < floor.length; i++) { for (int j = 0; j < floor[i].length; j++) { floor[i][j] = 0; } } prompt = "\nEnter below your set of commands: "; } public static void executeCommands () { int commandNum = 0; command = commandArray[ commandNum ][ 0 ]; // continue executing commands until either reach the end // or reach the max commands while ( command < 9 ) { // determine what command was entered // and perform desired action switch ( command ) { case 1: penDown = false; break; case 2: penDown = true; break; case 3: direction = rightTurn( direction ); break; case 4: direction = leftTurn( direction ); break; case 5: distance = commandArray[ commandNum ][ 1 ]; moveForward( penDown, floor, direction, distance ); break; case 6: System.out.println( "+=+=+=+=+=+=+=+=+=+=+\nYour computerized sketchpad: \n" ); printFloor( floor ); break; } // end switch command = commandArray[ ++commandNum ][ 0 ]; } // end while } // end method executeCommands // method to move the pen public static void moveForward( boolean down, int a[][], int dir, int dist ) { int j; // looping variable // determine which way to move pen switch ( dir ) { case 0: // move to right for ( j = 1; j <= dist && yPos + j < size; ++j ) if ( down ) a[ xPos ][ yPos + j ] = 1; yPos += j - 1; break; case 1: // move down for ( j = 1; j <= dist && xPos + j < size; ++j ) if ( down ) a[ xPos + j ][ yPos ] = 1; xPos += j - 1; break; case 2: // move to left for ( j = 1; j <= dist && yPos - j >= 0; ++j ) if ( down ) a[ xPos ][ yPos - j ] = 1; yPos -= j - 1; break; case 3: // move up for ( j = 1; j <= dist && xPos - j >= 0; ++j ) if ( down ) a[ xPos - j ][ yPos ] = 1; xPos -= j - 1; break; } // end switch } // end method moveForward
- 01-04-2012, 03:46 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
Everytime you execute a command it looks like you are incrementing the commandNum until eventually (assuming command < 9) you hit 30, which is too large for your array.
Why you are doing it this way I don't know as I don't know what this is supposed to be doing.
-
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
You could change this:
to this:Java Code:while ( command < 9 ) {
Might help...Java Code:while (command < 9 && commandNum < maxCommands) {
- 01-04-2012, 10:58 PM #4
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
You should test your code before posting it.commandNum < maxCommands)
The prefix operator will increment the index BEFORE using it.
Try this:
Why would you want to increment the index before testing it???Java Code:int maxCommands = 1; int[] anArray = new int[maxCommands]; int commandNum = 0; while (commandNum < maxCommands) { int x = anArray[++commandNum]; // ArrayIndexOutOfBoundsException: 1 }
-
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
I ran it in my head but obviously made this mistake. Bad habbit, sorry.
First run:
Java Code:int maxCommands = 1; int[] anArray = new int[maxCommands]; int commandNum = 0; while ( commandNum < maxCommands) { // 0 < 1 := TRUE int x = anArray[++commandNum]; /** * commandNum := 1 * x := anArray[1] * anArray.length = 1 (index 0) * ArrayIndexOutOfBoundsException (index 1, max index=0) */
Similar Threads
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
By junkDNA in forum New To JavaReplies: 3Last Post: 10-20-2011, 04:58 PM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
By dwill19686 in forum New To JavaReplies: 3Last Post: 02-04-2011, 08:31 PM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at wee
By Azaz in forum New To JavaReplies: 4Last Post: 02-02-2011, 04:32 AM -
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