When you call randomizer.nextInt(AnsArraylen)
you can get values between 0 and 3, inclusive. If this tempVal
is 0, you are fine, as you just set the AnsArray[x] to the current tempVal
. However, what happens when tempVal
is 1, for example? You go into none of those if
s, except for the last one, which sets x to -1, so on the next loop iteration it'll become 0 again. Besides whether this makes sense to you, the mistake is in the next line. You are in that case doing:
tempStringy=AnswerArray[AnsVal[0]];
but AnsVal[0]
is still -1
. You are therefore trying to do:
AnswerArray[-1]
That's where you are getting an Exception. Wrap that in a try-catch and print the stack trace to confirm. Perhaps you wanted to initialize AnsVal
as:
int[] AnsVal={0,1,2,3};