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) :
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
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 movePen
}// end of class Pro
-> Any suggestions on how I can solve this? Any form of help always appreciated. Thanks in advance.

