• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array out of bounds exception problem  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was about to finish my coding assignment, which is taking an array of predetermined "correct answers" and getting the user to enter in their answers and then giving them a pass or fail grade based on the answers given to me when I got the array out of bounds exception error. I have been pulling my hair out for the past hour trying to figure out what went wrong here I CAN NOT for the life of me figure it out. Any help is appreciated, thanks.

Here is my code:

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tyler, welcome to the Ranch!

If you look at the stack trace you got, you'll see it has more information than just the name of the exception. It also tells you the line of code which threw the exception and, in this case, it also tells you the array index which was out of bounds. So have another look at that, and if you're still stuck post back and let us know. Of course if you do that, make sure you post a copy of the stack trace as well. Good luck!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is caused by this this part of your code:



When you type



You're setting the entire array to the last char you typed, which means the array is only 1 char long which gives you your exception when you try to access any index higher than that later on in your code.

Instead I suggest you use



Hope this helped
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch SW

I can see another problem. Why have you got an array of chars representing the answers? Why do you not have a question class which has the answers attached and also tells which is correct? Why are you using a char for the answer? Why not number them and use 1 2 3 4? You would of course have to reject any numbers outwith that range.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!