• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory game ArrayIndexOutofBoundsException 5

 
Adam Boosned
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, my program compiles and runs great except for the very last display box/message.

It is suppose to give me the congrats message if all the colors are entered correctly, but I have a small out of bounds error that I can't figure out. Thanks in advance for any help offered.

here is the code:

 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It's a lot easier if you tell us where the exception occurs; I had to run it and got to "enter colour 4" and thought, "He has got a 5-element array and tried to use elements [1] to [5]." And lo and behold, when I tried to enter the 5th colour, the Exception appeared.

Remember: a 5-element arrya has nos [0] to [4]; there ain't no such thing as No [5].
 
Adam Boosned
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

It's a lot easier if you tell us where the exception occurs; I had to run it and got to "enter colour 4" and thought, "He has got a 5-element array and tried to use elements [1] to [5]." And lo and behold, when I tried to enter the 5th colour, the Exception appeared.

Remember: a 5-element arrya has nos [0] to [4]; there ain't no such thing as No [5].


Thanks for the reply. Ya i apologize, i should of mentioned the line of the code that is throwing the error. Also, i should of mentioned that after some research on this stuff (I am a beginner in code) hat the index == (colors.length +1) isn't going to work. But I am stuck on an alternative method.

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean, "index == (colors.length +1) isn't going to work?" It could "work," but it depends on what you want it to do.
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You start off your actionPerformed method with a double assignment to correct. This should be deleted: you can write boolean correct = colorInput[index].getText().equals(colors[index]); It would probably be better not to have that second array in the first place. Then you have your ++ operators in the wrong places. Again: why have you got an array at all? Why don't you simply change the text: you would need a String[] for different texts. That saves you setting things visible and invisible, but you may need repaitn() calls after setting the text.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!