User comparison

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 panel with a question with multiple answers to choose from, then the user is prompted to write an email with the correct answer. I did this and it works. But I want to compare the entered answer with the correct answer and only keep the number of correct answers in the text.file
If you can give any advice or sample code, I would really appreciate it. Thanks in advance and here is my code where I need to add this.

  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;
            }

        }

    }

      

+3


source to share


2 answers


What I suggest you do is read the question file and fill it in JLabel

, and then accept the appropriate answer.

Instead of a textBox

, you can have radioButtons

if there is only one answer.

If you have multiple options, you can use checkBoxes

.

How to create a shortcut



JLabel questionLabel = new JLabel(question);

      

How to create a radioButton.

JRadioButton optionA = new JRadioButton("A. "+ option[ 0 ]);
JRadioButton optionB = new JRadioButton("B. "+ option[ 1 ]);
JRadioButton optionC = new JRadioButton("C. "+ option[ 2 ]);
JRadioButton optionD = new JRadioButton("D. "+ option[ 3 ]);

ButtonGroup group = new ButtonGroup();
group.add(optionA);
group.add(optionB);
group.add(optionC);
group.add(optionD);

      

Now go through yours radioButtons

and get your option and check with the actual answer.

0


source


I made a similar application a few years ago. What I did was a question with answers in a text file. The first answer was always the right answer. When the question was shown on the screen (with its possible answers), I used the method to reorder the possible answers and memorized the number (or letter) of the correct answer. Then when I got the input from the user, it was easy to compare the answer with the correct answer.



In my application, I didn't ask for a letter, but I used radio buttons from which the user had to select 1. The advantage of keyboard input is that the user can use upper / lower case, or declare a space or so, which makes the answer wrong to correct answer. Radio buttons are much easier to compare as you get the number of the selected button, without unexpected additional input from the user.

0


source







All Articles