I created this Multiple Choice program and everything is fine and the right answer is printing out but I keep getting:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at MultipleChoices.main(MultipleChoices.java:21)
Can somebody tell me what I need to do to fix this error?
for(int i = 0; i < student[i].length; i++){
int rightAns = 0;
for(int j = 0; j < student[i].length; j++){
if(student[i][j].equalsIgnoreCase(key[j])){
rightAns++;
}
}
Your first for loop uses the wrong value. You should use student.length instead of student[i].
for(int i = 0; i < student.length; i++){
int rightAns = 0;
for(int j = 0; j < student[i].length; j++){
if(student[i][j].equalsIgnoreCase(key[j])){
rightAns++;
}
}
System.out.print("Student's " + i + "#correct answer: " + rightAns + "\n");
}
}