0
import java.util.Arrays;

/**
 * 
 */

/**
 * @author abogal6274
 *
 */
public class TicTacToe {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
TTTBoard();

    }
    public static void TTTBoard(){

        String[][] tttBoard = new String[3][3];
         for(int i=0; i<=3;i++){
             tttBoard[i][3] = "[ ]";
         }

         System.out.println(Arrays.deepToString(tttBoard));
    }

}



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at TicTacToe.TTTBoard(TicTacToe.java:25)
    at TicTacToe.main(TicTacToe.java:18)

The thing about this is that it says this as an error. Why? I tried like making it i<=3 , i<4 and all stuff but I still get error. I really need help on this to finish my tic tac toe App.

EDIT: Thats solved but: it prints out this: [[null, null, [ ]], [null, null, [ ]], [null, null, [ ]]] whitch its suppose to print but can it be vertical like a tic tac toe board?

eightShirt
  • 1,457
  • 2
  • 15
  • 29
  • change `for(int i=1; i<=3;i++)` to `for(int i=0; i<3;i++)` – rajuGT Nov 23 '15 at 12:48
  • @rajuGT Changed it but still dosent work... –  Nov 23 '15 at 12:50
  • tttBoard[i][3] = "[ ]"; should be tttBoard[i][2] = "[ ]"; – Raf Nov 23 '15 at 12:50
  • 4
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – wero Nov 23 '15 at 12:50
  • @Universe Also consider Raf's suggestion. Accessing 3rd index in 3*3 array is not valid. – rajuGT Nov 23 '15 at 12:51

5 Answers5

3

tttBoard[i-1][3] is out of bounds regardless of i, due to the 3.

If you wish to initialize the entire 2D array, use a nested loop :

    String[][] tttBoard = new String[3][3];
    for(int i=0; i<tttBoard.length;i++)
        for(int j=0; j<tttBoard[i].length;j++)
            tttBoard[i][j] = "[ ]";

To print the board in 3 lines, don't use System.out.println(Arrays.deepToString(tttBoard));.

You can use :

    for(int i=0; i<tttBoard.length;i++) {
        for(int j=0; j<tttBoard[i].length;j++)
            System.out.print(tttBoard[i][j] + ' ');
        System.out.println();
    }
Eran
  • 387,369
  • 54
  • 702
  • 768
  • @Universe Your array has 3 rows and 3 columns, so 3 is an invalid column index. – Eran Nov 23 '15 at 12:51
  • @Universe because the array range goes from `0-length-1`, which would result in `2` beeing the highest entry. In addition it looks like you would want to prepare the array for the game, which would result in 6 null entries in your array. – SomeJavaGuy Nov 23 '15 at 12:51
  • Wait it prints out this:[[null, null, [ ]], [null, null, [ ]], [null, null, [ ]]] whitch its suppose to print but can it be vertical like a tic tac toe board? –  Nov 23 '15 at 12:53
  • Ok Ima check that out does it print like a tic tac toe board though? –  Nov 23 '15 at 12:54
  • @Universe If you want to print the array in multiple lines, don't use `System.out.println(Arrays.deepToString(tttBoard));`. Use a nested loop and print the elements of the array one by one. – Eran Nov 23 '15 at 12:55
  • @Eran And How do i do that Eran? Sorry for my many questions . –  Nov 23 '15 at 12:56
  • @Eran Just used your code but it dosent work since you need to put that -1 after the tttboard[i].length(); But even after i did that it just prints null –  Nov 23 '15 at 12:59
  • @Universe You don't need a -1. I had a typo in the condition and increment of the inner loop (should be `j` instead of `i`). fixed now – Eran Nov 23 '15 at 13:03
1
String[][] tttBoard = new String[3][3];
         for(int i=1; i<=3;i++){
             tttBoard[i-1][3] = "[ ]";
         }

you have and array with 3 rows and 3 columns, both from index 0, 1 and 2, but you are trying to get the element at index 3 (the 4th element)

that is the reason of your Exception

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

you got this error because of that line tttBoard[i-1][3] = "[ ]";

Arrays are zero based ,if your array length is 1 then you can only reference element 0 so in your case you can reference 0,1,2.

i think what you need is tttBoard[i-1][2] = "[ ]";

karim mohsen
  • 2,164
  • 1
  • 15
  • 19
1

The exception is because you are accessing the out of bound in two places

for(int i=0; i<=3;i++){
             tttBoard[i][3] = "[ ]";
         }

In above loop replace i <= 3 to i < tttBoard.length

As well the following statement

tttBoard[i][3] = "[ ]";

3 in above causes the exception, max is 2. If you change it to tttBoard[i][2] = "[ ]"; then you won't get exception to avoid going beyond the array bound.

Raf
  • 7,505
  • 1
  • 42
  • 59
0

In java and most of other Programming language index counting start from 0.

So if you have an array of length 5 then index will be 0,1,2,3,4.

if you have an array of length N

index will be 0,1,2.....N-1. As you have an array like:

String[][] tttBoard = new String[3][3]

so indexes will be:

tttBoard[0-2][0-2]

for(int i=0; i<=3;i++)

inside the loop what happening is:

tttBoard[3][anything] ----> exception 

 tttBoard[anything][3] -> exception 
Saif
  • 6,804
  • 8
  • 40
  • 61