1

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++;
            }
        }

2
  • the problem is you are trying to get the 9th element of an array containing only 8 elements. Commented May 1, 2020 at 8:04
  • @Stultuske so how do i fix this error? Commented May 1, 2020 at 8:07

1 Answer 1

1

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");
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.