Random problem

Hey Guys,

I have a problem about how to generate randomly when the data consists of string and not integers. I wish to display a set of questions, which are stored in the textfile randomly. How can i do it? I know that i have to use the Math.Random but i don't know how to apply it since my data is string (since it is questions) and not numbers. If you can, pls can you give me an example using source code. Thanks a lot! :-)

Hm, well, when you read in the questions, you can store them in a String array, and, you can make it generate a String with a random index. For example

Java Code:

//Read in questions and store them to String[] questions
Random generator = new Random();
int randomIndex = generator.nextInt(questions.length);
//Generate a random number, and the maximum number it can be
//is the number of questions there are
System.out.println(questions[randomIndex]);

I can remember someone asking a question very similar to yours. I answered it and it has methods that you might find useful. One such method in one that can mix an array randomly, to prevent repetitions in the questions. Have a look at this tread.

I hope this helped. :D

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

Yeah it worked. Thanks a lot. My problem is now with the answers. Because, now that the questions come up randomly, the way that the answers are stored, the questions will be generated incorrectly since they won't match the answer. I have stored the answers of the questions in an array. What can i do? Thanks a lot.

Now you can create an array of QuizProblem objects and call it Quiz. You can modify the array mix method to work with the QuizProblem class or use generics if you want to. This way, you do not have to worry about data being lost or corrupted because of a changing data structure.

I hope this helped. :D

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

Yeah i have understood. Thanks a lot Man! I just got one last problem. My problem is that altough my program is popping up random questions, the problem is that it is sometimes displaying the same questions, since it is displaying them randomly. What can i do? Thanks a lot again! ;)

Mix method

I am sorry if I am repeating myself, but in the code I provided, the questions are loaded first, then you mix it using the mix method and then you can use the questions in incrementing order. That will prevent repetitions of the questions. The problem in your code lies here:

Oh i understood. Thanks. I tried doing it like this, (similar to the one were you posted to the other question) but its still not working, since its displaying some of the questions repeatedly. Do i have something wrong? Thanks a lot Man!

Mixing data structures

Okay mcal.

You are mixing data structures and your code presents problems for the future. The QuizProblem class makes things simple as they can get complicated if you use gibsonrocker800 approach. Lets stick with vectors as they have less limitations. You will need to use the QuizProblem class to prevent the question/answer mismatch problem. So lets put all your questions in one file and answers in another.

Java Code:

[B]Questions.txt Answers.txt[/B]
How did the skeleton cross the road? He didn't - cause he had no guts!
Do spiders breath through their mouths? No.
And so on... And so on...

Now we can modify the load method to create QuizProblem objects from the two files:

From somewhere in your program you can call these methods like this:
Vector<QuizProblem> quiz = mix(loadFile("questions.txt", "answers.txt"));
Now, you can ask each question in its order in the vector and not worry about:

The random factor.

Questions being repeated.

Questions having the wrong answers.

I hope this helped. There's a lot of code. :D (check by hand and likely problematic)

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

Ok thanks a lot. I have another problem. I have to use a binary file (instead of textfiles) to store (write) a the questions, which then i have to read using GUI. I have written one question on this binary file, but for the other 10 questions i have to do the same thing like the following code and if so can you pls tell me how to write the second question because when i tried doing it like the first one, it only showed me the first question. Thanks a lot. :-)

binary files

Hello mcal

You can create an object that can contain all your questions and answers. Then you can let it implement the Serializable interface. Now, using an ObjectInputStream or an ObjectOutputStream, you can easily save or load the object in one line of code.

Originally Posted by mcal

I have to use a binary file

Is this an assignment? You need to try and learn how to analyze your own code logically.

I will explain Object streaming if you want me to do that. ;)

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

Thanks a lot. Yeah this is an assignment, an the teacher told me instead of using a textfile i should use a binary file. And now i got a bit confused, because i have to alter everything. I had written the questions in a textfile, and now my teacher told me that i should write them on a binary file. And i don't know how to write multiple lines on a binary file. I had succesfully achieved in displaying a single question but i need to display the others. I did something like this. I am on the right track? It is issuing an EOFexception error. Thanks again for your help. :-)

Ok thanks. Just one last problem. Since before i was using a textfile i used the Buffered Reader. But now i have to use DataOutputStream and FileOutputStream. I have a little problem with this code, because i don't know how to change it so that it would write to a binaryfile. Can you pls help me out? Thanks a lot, and i'm really sorry if i have bothered you. I have tried many times to change it but i really can't do it. Thanks again.

Java Code:

try {
BufferedWriter out; // what do i have to write her instead of Buffered Reader
String text = JOptionPane.showInputDialog(null, "Enter new question");
out = new BufferedWriter(new FileWriter("plate_tectonics.dat",true));
out.write(text); //Writing to the textfile (the name entered by user)
out.newLine(); //write a new line to the textfile so the next time it writes to the file it does it on the next line
out.close();
}catch(IOException event){
System.out.println("There was a problem:" + e);
}

indentation and minor errors

Hello

Your code is correct except for the event part. It should be a "e" and the getMessage() call is missing.

Java Code:

try {
[COLOR="Teal"]BufferedWriter out = new BufferedWriter(new FileWriter("plate_tectonics.dat",true)); // Assuming you want to append to the file[/COLOR]
String text = JOptionPane.showInputDialog(null, "Enter new question");
out.write(text); //Writing to the textfile (the name entered by user)
out.newLine(); //write a new line to the textfile so the next time it writes to the file it does it on the next line
out.close();
}catch(IOException e [COLOR="Red"]/*event*/[/COLOR]){
System.out.println("There was a problem: " + e.[COLOR="Blue"]getMessage[/COLOR]());
}

Also, try to use better indentation. It improves the readability of your code.

Hope that helped. ;)

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