0

I can't figure out what the error is.

public void quizTest ()
    {
        c = new Console();
        int total = 34;
        String[] quizArray = new String [total];
        String quizQuestions;
        int i = 0;
        int u = 0;

        try
        {
            FileReader readQuestion = new FileReader ("QuizQuestions1.txt");
            BufferedReader buffReadQuestion = new BufferedReader (readQuestion);

            while ((quizQuestions = buffReadQuestion.readLine ()) != null)
            {
                quizArray [u] = quizQuestions;
                u++;
            }


            for (i = 0 ; i > u ; i++)
            {
                c.println (quizArray [i]);
            }
        }
        catch (IOException e)
        {
            new Message ("Error");
        }

    }

Here's the error message:

java.lang.ArrayIndexOutOfBoundsException: 34
at BooleanLogic.quizTest(BooleanLogic.java:342)
at BooleanLogic.main(BooleanLogic.java:367)

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Daigan
0
for (i = 0 ; i > u ; i++)
            {
                c.println (quizArray [i]);
            }

should be changed to

for (i = 0 ; i < quizArray.length ; i++)
            {
                c.println (quizArray [i]);
            }

Actually checking quizArray.length will make sure that you do not get that exception, and, when you change the amount of quiz questions (the total variable), you won't have to change anything else.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.