-1

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?

4castle
  • 30,756
  • 11
  • 66
  • 100
superuser0
  • 125
  • 1
  • 1
  • 12
  • Have you tried stepping through the code in your debugger? – Jason May 13 '14 at 04:02
  • Can you please include the exact stack trace so that we can see which line encounters the error? – Jason May 13 '14 at 04:02
  • @Makoto This problem is not a duplicate because the zero-based indexing is being handled (as in `col - 1` and `row - 1`). – Jason May 13 '14 at 04:11
  • I disagree. First, the canonical answer is, "You're indexing off by one spot." Second, both `row` and `col` start at `1`. Third, there is a chance that the incrementation will step off of the array. – Makoto May 13 '14 at 04:13
  • But I still can't see where the problem lies. @superuser0 are you sure you posted the code with the problem? – Jason May 13 '14 at 04:14
  • @Makoto No, because the only place that can access the arrays is passed `col - 1` and `row - 1`. – Jason May 13 '14 at 04:14
  • Yes, but those if blocks don't check boundaries on `col`. For instance, if `col == 10` and `ch` is between 1 and 9, then you will invoke the case in which you attempt to add to the array. – Makoto May 13 '14 at 04:15
  • @Makoto - still not a duplicate because col can't be 10 at the beginning of the loop. – Jason May 13 '14 at 04:16
  • @superuser0 Please check that you've posted the correct code. Otherwise we can't help you (and we've fought to keep your question open). – Jason May 13 '14 at 04:21
  • @Makoto - oops, sorry, you were too quick for me! – Dawood ibn Kareem May 13 '14 at 04:21
  • @DavidWallace: I was about...seven minutes ago. Digging into this, yes, I see that this wasn't a duplicate. I'm not even sure if it can be reproduced. – Makoto May 13 '14 at 04:22
  • I've built the simplest version of your code possible, and it works fine. – Jason May 13 '14 at 04:25
  • @Makoto, Yeah, but the page doesn't show that the question has been reopened, until the user posts something. Kind of a bug in SO, really. – Dawood ibn Kareem May 13 '14 at 04:35
  • @DavidWallace: Fair enough. Time to temper myself from now on... – Makoto May 13 '14 at 04:37
  • 3
    Don't vandalize your question. If you think it should be deleted, click the little "delete" link, right next to the "edit" link. – Nic Jan 28 '17 at 05:22

1 Answers1

1

Here's my version of your code (so that it can be compiled and tested - I removed reference to In and replaced with hard-coded value '2'):

public class OffByOne {

    public static void main(String[] args) {
        Field field = new Field();
        int col = 1;
        int row = 1;
        do {
            char ch = '2';//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);

        System.out.println("Done");
        //In.readLine();
        //        return field;
    }
}

And

public class Field {
    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;
    }
}

I compiled it and it ran perfectly. It did not produce any exception.

Jason
  • 11,448
  • 3
  • 41
  • 46
  • OK - you are right and I was wrong, my exception was indeed caused by a different call of the the array. This issue can be closed... – superuser0 May 13 '14 at 05:02
  • @superuser0 Maybe you can just delete your question then. – Dawood ibn Kareem May 13 '14 at 05:42
  • @superuser0 You may want to consider standardising your indexing numbering across your code. If you choose 1-based indexing, then hide the `col - 1` and `row - 1` inside `Field.initializeCell()` (and check for invalid indexes there too). – Jason May 13 '14 at 06:38