For loop problem

I'm really stuck. I am making a quiz, where questions are stored in a binary file. The answers are stored in an array. My problem comes when i have to compare the answer entered by the user to the correct answer. The correct answers are stored in an array. I tried to do a for loop, but its not working. (Infact everything inside the for loop isn't working) Can someone pls help me because i really got stuck and i can't continue programming with such a problem. Thanks a lot. :-)

Hey thanks a lot. But i think the problem is when it comes to compare the answers. I have a problem, because i was first using a textfile, and now my tutor told me that i should use a binary file to store the questions. And i really got confused of how am i going to arrange my program. Do write to a binary file do i have to write each question in the java source code? Because i think in this way there will be a lot of repetitive code. And if so how am i going to compare the answer with each correct answer? II'm really confused. Thanks for your help. You'r really a great person. :-) Thanks again.

Thanks. That helped a lot. I just got a problem. Because, i don't know why but the for loop isn't working. So the counter isn't being incremented and when time is over it still continues displaying the questions. It doesn't even check whether the answers are correct or not. Do i have something wrong in the for loop? Thanks a lot for your help. :-)

the for loop isn't working
Let's have a look at the askQuestions method.

Java Code:

public void askQuestions(String[] questions, String[] answers) {
// To begin with, are you certain that the questions array
// and the answers array are the same length and have non-null
// elements? If so, okay. If not, this is a good place to check.
// See comment below this method for more about this.
int count = 0;
int point = 0;
for(int j = 0; j < questions.length; j++) {
// The "timeForMore" is a member variable (declared in
// class scope) and is used as a flag, set in the
// [i]stopAndShowResults[/i] method by another class.
// If you set it to [i]true[/i] for each trip thru this
// loop you may interfere with its function which is to
// wait until the timing class notifies this class that
// the time is up and we should break out of the
// 'askingQuestions' loop. This should be reset when you
// start timing each question_and_answer session; perhaps
// when you enter this method but possibley not. So this
// next line should be removed from here.
timeForMore = true;
// You will get better results with a Random instance if
// you declare it as a member variable (in class scope)
// rather than making a new one for each trip thru this
// loop. So move this next line to the member variable
// declarations area up above the constructor.
Random generator = new Random();
// Question for later - how will you prevent "generator"
// from returning duplicate indices?
int randomIndex = generator.nextInt(questions.length);
String input = JOptionPane.showInputDialog(null, questions[randomIndex]);
[i]the counter isn't being incremented[/i]
// There are no curley braces after this [i]if[/i]
// statement to form a code block so the next statement,
// viz, "count++;", is the only statement affected/protected
// by this [i]if[/i] statement. If you add a curley brace
// after the [i]if[/i] statement and after the "point++"
// statement then boith statements will be
// included_in/controlled_protected_by the [i]if[/i] statement.
if(answers[randomIndex].equalsIgnoreCase(input))
count++; // incrementing counter if entered answer is correct
point++;
// if time is over, the program executes the loop an stops
// asking questions.
if(!timeForMore)
break;
}
JOptionPane.showMessageDialog(null, "You answered " + count +
" out of " + questions.length +
" questions correctly.");
int percent = (count*100)/questions.length;
JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " +
percent + " % ");
}

I asked about the "questions" array argument to the askQuestions method because of this:

Java Code:

try {
while (true) {
// Here you are saving the read input in the "desc" variable but
// you do not add it ("desc") to the StringBuilder. So when the
// StringBuilder is returned as a string it will be empty.
desc = in.readUTF();
}
} catch (EOFException e) { }

when time is over it still continues displaying the questions
Could be many things. First remove the reassignment of "timeForMore" from the for loop, mentioned above. Then, as a place to start, since we are working in/on the askQuestions method, try printing out the value of "timeForMore" in the "askQuestions" for loop to see if the timing class changes this flags value when the time has expired.

It doesn't even check whether the answers are correct or not.
This could be because of a zero–length array being passed to the askQuestions method as the "questions" argument.
Note: I see you have made an edit since I started writing this.

It is just showing: "You answered 0 out of 1 questions correctly". How come it is saying one question when i have 10 questions? I really got confused. Thanks a lot for your help. You are really kind, and i really appreciate your help. When i was working with the textfile, the program worked fine, but now (with the binary file) it isn't.

it just skips the for loop ... How come it is saying one question when i have 10 questions?

Back to the while loop in the three readFileXXX methods in which you do not save the data you are reading from the file into the StringBuilder.

Java Code:

try {
while (true) {
// Here you are saving the read input in the "desc" variable but
// you do not add it ("desc") to the StringBuilder. So when the
// StringBuilder is returned as a string it will be empty.
desc = in.readUTF();
// Need to add the newly-read data to the StringBuilder:
sb.append(desc + separator);
}
} catch (EOFException e) { }

To test/explore this idea/suggestion try adding a print statement at the beginning of the askQusestions method to check the incoming arguments, like this:

Ok thanks a lot. Just one last problem. When i removed the string builder, now it is issuing a missing return statement. What is missing? And it is always displying the questions 2 times. First it displays the questions, and then for a second time it display them again. Thanks a lot. And i really have to thank you for your help. Thanks again. :-)

Don't remove the StringBuilder. That is what is holding and sending (returning) the information (that the method reads from the file) back to the caller. The caller (the ActionListeners actionPerformed method) needs this information to send to the askQuestions method.
Also, since the readFileXXX method(s) declare a return type of type "String" (vis–a–vis "null", ie, nothing to return to the caller) the method must return a value of type "String". The compiler is keeping us out of trouble.

is always displying the questions 2 times
Maybe the askQuestions method is being called more than once or there may be some logic glitch in the method or maybe two sets of questions were sent into the method in the "questions" array.

Hi, i'm really sorry but i got stuck with an other problem. As you have told me previously, the method of random i have used isn't so efficient since it is displayin some of the question repetively. Is there another way, how i could generate the questions randomly without displaying some of the questions over again? Thanks a lot. I'm really obliged.

Old thread

Hello mcal

You have previously created a thread on this quiz problem. I have given you a solution for the repetitions. Please consider it. If you have problems with polymorphism or some of my methods, please inform us so that we can help you.

Good luck. ;)

Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Yeah i know, and i thank you for that. But to tell you the truth i didn't understand it, because i'm still a beginner, and i have never worked with vectors before. I really thank you, for helping me with that, but i wish that i would find something more simpler. Thanks Tim, i appreciate it a lot. Infact i posted again, because i'm trying to find a simpler method.

Now, we can create an array of QuizProblem objects from a method. But, since you want to use arrays, we must know how many questions there are. I will assume that for every question you have an answer in an array. You have already loaded the questions in an array so now we have all we need.
Look at this method:

I'm a lit bit afraid to make that change because i have spend a lot of time trying to make it work, and i soon have to give this assignment to my tutor. Isn't there maybe another way where the questions can be generated randomly without making a lot of changes in my program? Thanks a lot. I'm really sorry that i can't follow that advice but i'm a bit afraid of changing it. I wish that i could include a random method in the for loop, that can generate the questions randomly without repeating them. I'm really obliged to both you and Hardwired for helping me. If pls, anyone one of you, knows a shorter way of how to make the questions generated randomly, without having to make a lot of changes in my program, i would really appreciated. Thanks again. :-) And i'm really thankful.

Random index

Hello mcal

That is unfortunate. There is another solution that will needed to be added to this method and it will not require that you alter a lot of your program. You are trying to get a random index, but you repeat indexes. The solution is to create an array, A, that contains the integers of all the indexes possible in incrementing order. Then we create a second array that will contain the final "mixed" indexes, B. Both A and B will need counters to monitor their actual size.
Now add this code before your for loop: