0

I need to print this array for a tic tac toe program I am writing for APCS. I am getting an ArrayIndexOutOfBoundsException.

    String[][] ticBoard = {
        {"-","-","-"},
        {"-","-","-"},
        {"-","-","-"}
    };

    for(int d = 0; d < ticBoard.length; d++){
        for(int r = 0; r < ticBoard.length; d++){
            System.out.print(ticBoard[d][r]);
        }
    }
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
maxcodes
  • 544
  • 6
  • 15
  • 1
    Just change `d++` to `r++` in the inner loop. Note that although it works, it might fail if number of rows is different. Your inner loop should run on each row separately, `ticBoeard[d]` represents the row you're iterating on. – Maroun Nov 26 '14 at 21:18
  • 3
    `for(int r = 0; r – Alexis C. Nov 26 '14 at 21:19
  • Or use `System.out.println(Arrays.deepToString(ticBoard));` – Maroun Nov 26 '14 at 21:20
  • Note that your code, even after changing to `r++`, works only for square 2-D arrays. That's because you let `d` go from 0 to the number of rows minus 1, but then you let `r` also go from 0 to the number of **rows** minus 1, instead of the number of columns. Better would be `r < ticBoard[d].length`. – ajb Nov 26 '14 at 21:21
  • 1
    Also, you probably want `System.out.println()` after printing each row, to go to the next line. – ajb Nov 26 '14 at 21:21
  • @MarounMaroun Since this is for a tic-tac-toe program, I don't think the output provided by `deepToString` will help. – Alexis C. Nov 26 '14 at 21:22
  • @ZouZou Thay way OP is displaying contents now will be exactly like using `deepToString`. – Maroun Nov 26 '14 at 21:23
  • @MarounMaroun Yes but I don't think this is the desired behavior. – Alexis C. Nov 26 '14 at 21:24

3 Answers3

4

You should change d++ to r++ like @Maroun Maroun said but also:

    for(int d=0; d<ticBoard.length;d++){
       for(int r = 0; r<ticBoard[d].length;r++){
           System.out.print(ticBoard[d][r]);
       }
       System.out.println();
    }

Just incase your number of rows doesn't equal your number of columns.

brso05
  • 13,142
  • 2
  • 21
  • 40
2

You're using the wrong limit in the inner loop; you should be using the length of a row, not the number of columns. (Since your two-dimensional array doesn't have the same number of rows and columns, this is especially noticeable.)

Because of this, you're going off the end of the first row of the array when you try to access the 4th element. Your code specifies a maximum value for the column index of ticBoard.length (i.e. 4), which does not correspond to the actual number of items in that row (i.e. 3).

This can be fixed by looping up to the number of elements in the row (i.e. ticBoard[d].length), not the number of rows in the array (i.e. ticBoard.length)

Furthermore, you're incrementing the wrong value in the inner loop; it should be r, not d.

for(int d = 0; d < ticBoard.length; d++) {
    for(int r = 0; r < ticBoard[d].length; r++) {
        System.out.print(ticBoard[d][r]);
    }
    System.out.println(); // So that each new row gets its own line
}
APerson
  • 8,140
  • 8
  • 35
  • 49
0

This,

for(int r = 0; r < ticBoard.length; d++){

should be something like

for(int r = 0; r < ticBoard[d].length; r++){

Or, you could use Arrays.deepToString(Object[]) to print the 2d array like

System.out.println(Arrays.deepToString(ticBoard));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249