mercredi 6 juin 2018

Quiz game in Netbeans - reading from text file

i am making a quiz game in NetBeans and i need a little bit of help. I want to read the questions from a .txt file and so the answers and setText on label for the question and radio buttons for the answers. I have a txt file layed out like that:

"1 Question..?"

"first possible answer"

"second possible answer"

"third possible answer"

"fourth possible answer"

"CORRECT answer"

"2 Question..?"

"first possible answer"

"second possible answer"

"third possible answer"

"fourth possible answer"

"CORRECT answer"

and so on..

I managed to create a string for each (question, first answer...) where i save the text from the file and it displays it for me in label 1, radio button 1,... The program displays everything correct, but i am wondering how can i display each group (question,answer1,..) randomly and so that they don't repeat.

String question;
String first;
String second;
String third;
String fourth;
String correct;

private void jBtnStartActionPerformed(java.awt.event.ActionEvent evt) {                                          

    int x=1, i=1;
    try {
        BufferedReader br=new BufferedReader(new FileReader("questions.txt"));
        while(br.ready()){
            if(i==x){
                question=br.readLine();
                first=br.readLine();
                second=br.readLine();
                third=br.readLine();
                fourth=br.readLine();
                correct=br.readLine();
            }
            else{
                question=br.readLine();
                first=br.readLine();
                second=br.readLine();
                third=br.readLine();
                fourth=br.readLine();
                correct=br.readLine();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(Okno2.class.getName()).log(Level.SEVERE, null, ex);
    }

    jLabel1.setText(question);
    jRadioButton1.setText(first);
    jRadioButton2.setText(second);
    jRadioButton3.setText(third);
    jRadioButton4.setText(correct);
}




Aucun commentaire:

Enregistrer un commentaire