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)

Recommended Answers

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 …

Jump to Post

All 2 Replies

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.

Oh thanks. That fixed it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.