Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.ArrayIndexOutOfBoundsException error.

807600Aug 11 2007 — edited Aug 11 2007
hello. :)

so here's the thing. this chunk of code has been bothering me for sometime. I'll try to explain what is wrong as best as I can. I'm new to this, so... expect my code to be made of fail.

here's the error I'm getting:
java.lang.ArrayIndexOutOfBoundsException: 6
at bingobingo.bin_go.Cards(bingo.java:207)



basically want I want to do is fill a 5x5 matrix with numbers created in GeneraNum(). I want to fill each space of the matrix with a different number, it can't have more than one number repeat itself.
whenever it compiles, it throws the error I posted above. I've tried a thousand different ways and it always gives me that error.

207 would be this line:
card5[i+counter][j+counter] = num5;
it's an awful thing to type in, haha, but I was completely out of ideas.

I'm already aware I could replace the if and else part with the random() line, but if I do it that way some numbers appear more than once in the card and I don't want that.

thanks for taking the time to look. :]
// Generates a random number.
 void GeneraNum() {
        num5 = 1 + (int) (Math.random() * 99);
  }

  // Creates the bingo cards

  void Cards() {

    salida5 = "";

    GeneraNum();

      for (i = 0; i <= 4; i++) {
        for (j = 0; j <= 4; j++) {
            if (card5[i][j] != num5) {
              card5[i+counter][j+counter] = num5;
              counter++;
            }
            else if (card5[i][j] == num5) {
              GeneraNum();
            }
        }
      }

    JOptionPane.showMessageDialog(null, salida5, "Card 5",
                                  JOptionPane.INFORMATION_MESSAGE);
  }
Message was edited by:
acid_food
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 8 2007
Added on Aug 11 2007
6 comments
1,232 views