Stuck on this bit of code
<p>What I am doing is creating a command line "game" where there is a 3x3 grid (Array) where you can move a "1" through it by typing the direction (up, down, left, right). </p>

<p>For example:</p>

<p>0 0 0</p>

<p>0 1 0</p>

<p>0 0 0</p>

<p>I've made it so if the 1 is on the edge of the array it is not allowed to move out of the boundaries (read: resulting in an out of index error).</p>

<p>I'm completely lost as whenever I try to move right, I receiving the following:</p>

<pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Logic.setMove(Logic.java:87)
at Logic.getMove(Logic.java:10)
at GridGameMain.main(GridGameMain.java:13)
</code></pre>

<p>Here's my code:</p>

<pre><code>public class GridGameMain {
static int[][] board = new int[3][3];
public static void main(String[] args){

    board[(int) (Math.random() * 2.5)][(int) (Math.random() * 2.5)] = 1;
    for (int i =0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
        System.out.print(" " + board[j][i]);
        }
        System.out.println("");
        }
    Logic l = new Logic();
    l.getMove();

}

}


import java.util.Scanner;

public class Logic extends GridGameMain{
void getMove(){     //takes user input then calls setMove

    String direction;  //string to hold the direction
    Scanner user_input = new Scanner(System.in);   
    direction = user_input.next();    
    Logic l = new Logic();      
    l.setMove(direction);        

}
void setMove(String direction){ //called when user presses enter upon typing a move
    Logic l = new Logic();

    if(direction.equals("up")){

        if(board[0][0] == 1 || board[1][0] == 1 || board[2][0] == 1 ){

            System.out.println("Invalid move!");

            l.getMove();

        }else{
        for(int a = 0; a &lt; 3; a++){
            for(int b = 0; b &lt; 3; b++){
                if(board[a][b] == 1){
                    board[a][b-1] = 1;
                    board[a][b] = 0;
                    break;
                }
            }
        }
        l.printBoard();
        System.out.println("you moved up");
        l.getMove();
        }
    }

    if(direction.equals("down")){           
        if(board[0][2] == 1 || board[1][2] == 1 || board[2][2] == 1 ){
            System.out.println("Invalid move!");
            l.getMove();
        }else{
            for(int a = 0; a &lt; 3; a++){
                for(int b = 0; b &lt; 3; b++){
                    if(board[a][b] == 1){
                        board[a][b+1] = 1;
                        board[a][b] = 0;
                        break;
                    }
                }
            }
            l.printBoard();
        System.out.println("you moved down");
        l.getMove();
        }
    }
    if(direction.equals("left")){
        if(board[0][0] == 1 || board[0][1] == 1 || board[0][2] == 1 ){
            System.out.println("Invalid move!");
            l.getMove();
        }else{
            for(int a = 0; a &lt; 3; a++){
                for(int b = 0; b &lt; 3; b++){
                    if(board[a][b] == 1){
                        board[a-1][b] = 1;
                        board[a][b] = 0;
                        break;
                    }
                }
            }
            l.printBoard();

        System.out.println("you moved left");
        l.getMove();
        }
    }
    if(direction.equals("right")){
        if(board[2][0] == 1 || board[2][1] == 1 || board[2][2] == 1 ){
            System.out.println("Invalid move!");
            l.getMove();
        }else{
            for(int a = 0; a &lt; 3; a++){
                for(int b = 0; b &lt; 3; b++){
                    if(board[a][b] == 1){
                        board[a+1][b] = 1;
                        board[a][b] = 0;
                        break;
                    }
                }
            }

        l.printBoard();
        System.out.println("you moved right");
        l.getMove();
        }
    }
}
void printBoard(){
    for (int i =0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
        System.out.print(" " + board[j][i]);
        }
        System.out.println("");
        }
}
}
</code></pre>

<p>I'm just not sure why I can't move right when I can move up, down, and left just fine. Please tell me I'm not crazy! </p>
