Greg Flint
Greg Flint

Reputation: 33

Array out of Bounds Error Java

This first code is all I believe you need to look at. The rest of the code is in case there is something I am missing.

Edit: Indeed changing that second j to a k fixes the problem. I was looping the j twice and the values were getting too large for my array. Thank you!

public static void makeMove(){
    char[] path = new char[TicTacToeArray.length];
    int[][] DefensiveOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length];

    for(int i=0; i < DefensiveOppsArray.length; i++){
        for(int j=0; j < DefensiveOppsArray.length; j++){
            DefensiveOppsArray[i][j] = 0;
        }
    }   

    for(int i=0; i < DefensiveOppsArray.length; i++){
        for(int j=0; j < DefensiveOppsArray.length; j++){
            //path for straight down
            for(j=0; j < DefensiveOppsArray.length; j++){
                path[j] = TicTacToeArray[i][j];}

                DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;


            }
        }
    }

I've been working on this assignment for a tic tac toe Java game but have gotten stuck with "Exception in thread "main" java.lang.ArrayIndexOutofBoundsException: 3. This error comes from the line DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;. This must mean that I somehow have not properly defined the size of my DefensiveOppsArray. What am i doing wrong?

TicTacToeArray is inherited from UserTicTacToe. The test class is simply

public class Test3{

public static void main(String[] args){
    IntelligentTicTacToe2.promptUserTTT();
    }
}

I need to create a DefenseOppsArray of the same length as the TicTacToeArray so that I can manipulate the numbers so I can determine which the best move is. Unfortunately I am struggling with simply creating and manipulating the numbers within the DefenseOppsArray.

public class IntelligentTicTacToe2 extends UserTicTacToe{

public static void promptUserTTT(){

    //read the input size
    System.out.print("Enter TicTacToe Array Size: ");
    int size = UserInput.readInt();
    System.out.println("");

    //start the game
    UserTicTacToe.startTTT(size);

    //keep track of consecutive errors
    int consecutiveErrors = 0;

    //display the initial game board
    UserTicTacToe.displayTTT();

    //let the user keep playing forever if they want to
    while(true){
        //get the input symbol from the user
        System.out.print("Enter Symbol (X or O): ");
        String symbol = UserInput.readString();
        System.out.println("");
        //hopefully the string is just one character - if not get just
        //the first character
        char sym = '*';
        if(symbol.length() > 0){
            sym = symbol.charAt(0);
        }

        //if the symbol was a Q, then quit
        if(sym == 'Q'){
            break ;
        }

        //get the row and column
        System.out.print("Enter Row to Place Symbol: ");
        int row = UserInput.readInt();
        System.out.println("");
        System.out.print("Enter Col to Place Symbol: ");
        int col = UserInput.readInt();
        System.out.println("");

        //update the game board and see if input was valid
        boolean inputValid = UserTicTacToe.updateTTT(sym,row,col);

        //re-display the game board if input was accepted
        if(inputValid){
            UserTicTacToe.scoreTTT();
            UserTicTacToe.displayTTT();
            consecutiveErrors = 0;
        }
        //if input was rejected, print a message, increment error count,
        //and quit if we are on the 5th error
        else{
            System.out.println("Invalid Input!");
            if(consecutiveErrors >= 4){
                break ;
            }
            consecutiveErrors++;
        }
        makeMove();



    }
}

Upvotes: 0

Views: 1678

Answers (3)

Rob Watts
Rob Watts

Reputation: 7146

The problem is indeed the nested for loop that reuses j as its variable. That part of the code should be formatted like this:

for(int i=0; i < DefensiveOppsArray.length; i++){
    for(int j=0; j < DefensiveOppsArray.length; j++){
        //path for straight down
        for(j=0; j < DefensiveOppsArray.length; j++){
            path[j] = TicTacToeArray[i][j];
        }

        DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
    }
}

This makes it clear what's happening - j is being increased to 3 in the inner for loop, so when it hits that next line it throws the exception. I don't know if you meant for the line

DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;

to be in that inner for loop or not, but do not reuse j in that inner for loop even if you really do need the loop.

Upvotes: 1

J.Miller
J.Miller

Reputation: 487

You are incrementing int j twice.

Upvotes: 1

DeltaLima
DeltaLima

Reputation: 5944

The problem is that you use j twice as counter in nested loops

This is where it goes wrong:

for(int i=0; i < DefensiveOppsArray.length; i++){
    for(int j=0; j < DefensiveOppsArray.length; j++){
        //path for straight down
        for(j=0; j < DefensiveOppsArray.length; j++){ // << here you use j again as counter
            path[j] = TicTacToeArray[i][j];
            DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
        }
     }
}

Perhaps you have coded one loop too many?

Upvotes: 2

Related Questions