• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array out of bounds exception problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic