I am trying to fill a 9x9 array via input from the user:
Field field = new Field();
int col = 1;
int row = 1;
do {
char ch = In.readChar();
if (ch == '-' || ch == '0') {
col++;
} else if (ch >= '1' && ch <= '9') {
field.initializeCell(col - 1, row - 1, ch - '0');
col++;
}
if (col > 9) {
row++;
col = 1;
}
} while (row <= 9);
In.readLine();
return field;
Class field has
public int[][] cellValue = { {0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0}};
public void initializeCell(int col, int row, int value) {
this.cellValue[col][row] = value;
}
Now this allows for only 80 chars, for example
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
12345678
and adding this last character produces an
java.lang.ArrayIndexOutOfBoundsException: 9
Can anyone explain my mistake to me?