I have a text.file with questions and another text.file with the correct answers to the questions. My program contains some JButton-s and when the user clicks on the button it shows a new pane with a question with multiple choice answers, then the user is asked to write the letter of the correct answer. I have done this things and it works. But I want to compare the entered answer with correct answer and store only the number of correct answers in a text.file
If you can give some advice or an example code i will really appreciate it. Thank you in advance and here is my code, where I need to add this things.
JTextField xField = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Answer: "));
myPanel.add(xField);
myPanel.add(Box.createHorizontalStrut(20));
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter your Answer", JOptionPane.OK_CANCEL_OPTION);
return;
}
}
}
What I suggest you to do is to read the file of the questions and populate it in a
JLabel
, and then take it's respective answer.We instead of having a
textBox
you could haveradioButtons
, If there is only a single answer.In case you have multiple options, you can use
checkBoxes
.How to create a Label
How to create a radioButton.
Now loop through your
radioButtons
, and get your option and check with the actual answer.