Results 1 to 5 of 5
  1. #1
    7stripes is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default 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) :


    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
    -> Any suggestions on how I can solve this? Any form of help always appreciated. Thanks in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default 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.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

    You could change this:

    Java Code:
    while ( command < 9 ) {
    to this:

    Java Code:
    while (command < 9 && commandNum < maxCommands) {
    Might help...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

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

    commandNum < maxCommands)
    You should test your code before posting it.
    The prefix operator will increment the index BEFORE using it.
    Try this:
    Java Code:
            int maxCommands = 1;
            int[] anArray = new int[maxCommands];
            int commandNum = 0;
            while (commandNum < maxCommands) {
                int x = anArray[++commandNum];  // ArrayIndexOutOfBoundsException: 1
            }
    Why would you want to increment the index before testing it???

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

    Quote Originally Posted by Norm View Post
    You should test your code before posting it.
    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

  1. Replies: 3
    Last Post: 10-20-2011, 04:58 PM
  2. Replies: 3
    Last Post: 02-04-2011, 08:31 PM
  3. Replies: 4
    Last Post: 02-02-2011, 04:32 AM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Tags for this Thread

Posting Permissions

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