This is the hardest thing I have tried yet. I am not adding ti as part of the main programme I am doing at the moment cus otherwise it would be too hard to debug. So I have created this as a separate file. I am trying to get a Joptionpane to show ask a question with 2 radio buttons to select the answer. IF the correct answer is shown then another JOptionpane will show saying correct. I also have with this a way to record correct questions you will understand better from the code. once the correct or incorrect box for selecting the wrong answer shows then when the user presses okay it goes to the next question. I am only starting with two questions to keep it simple then once thats mastered I will create 40. anyway here is my code. so anything I am doing wrong or need to do please show me. my biggest problem is an error saying incompatible types. it is cus I am trying to make an equal between a radiobutton and a string but I dont know how else to do it.


import javax.swing.*;

public class QuizFourBib
{
public static void main (String args[])
{
int res = 0;
JRadioButton dav = new JRadioButton("David");
JRadioButton saul = new JRadioButton("saul");
String ques1 = JOptionPane.showInputDialog(null,"who killed goliath?"+dav+saul,"question1",JOptionPane.QUESTION_MESSAGE);

if (ques1 == dav)
{
res=res+1;

}
else if (ques1 == saul)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
JRadioButton egypt = new JRadioButton("egypt");
JRadioButton samaria = new JRadioButton("samaria");
String ques2 = JOptionPane.showInputDialog(null,"where was moses born?"+egypt+samaria,"question1",JOptionPane.QUESTION_MESSAGE);
if (ques2 == egypt)
{
res=res+1;

}
else if (ques2 == samaria)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println(res);
}


}

Well, you can't quite work with a custom input dialog like that. There is a way to do it with an Object array for responses, but if you want radio buttons you need to create your own custom dialog. Just for the heck of it, I wrote up one way you could put this all together with JDialogs. Of course, you could probably do it a bit easier with a simple panel. Perhaps this will give you some ideas to work from (the simple BoxLayout is pretty awful)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.*;

public class QuizFourBib extends JDialog {
        List<Question> questions = new ArrayList<Question>();
        int score=0;
        ButtonGroup group = new ButtonGroup();
        JButton btnOk = new JButton("Ok");
        
        public QuizFourBib(){
            super();
            setModal(true);
            setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
            btnOk.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            
            questions.add(new Question("Who killed Goliath?", new String[]{"David", "Saul"}, "David"));
            questions.add(new Question("Where was Moses born?", new String[]{"Egypt","Samaria"}, "Egypt"));
        }
    
        public int startQuiz(){
            int score=0;
            for (Question q : questions){
                displayQuestion(q);
                if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){
                    score++;
                }
            }
            dispose();
            return score;
        }
        
        private void displayQuestion(Question q){
            getContentPane().removeAll();
            for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){
                group.remove((AbstractButton)buttonGroup.nextElement());
            }
            
            JLabel questionText = new JLabel(q.getQuestion());
            getContentPane().add(questionText);
            for (String answer : q.getAnswers()){
                JRadioButton radio = new JRadioButton(answer);
                radio.setActionCommand(answer);
                group.add(radio);
                getContentPane().add(radio);
            }
            getContentPane().add(btnOk);
            pack();
            setVisible(true);
        }
        
    public static void main(String args[]) {
        QuizFourBib quiz = new QuizFourBib();
        int score = quiz.startQuiz();
        
        JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 
    }
    
    class Question {
        String question="";
        String[] answers;
        String correctAnswer;
        
        public Question(String question, String[] possibleAnswers, String correctAnswer){
            this.question = question;
            this.answers = possibleAnswers;
            this.correctAnswer = correctAnswer;
        }

        public String getQuestion() {
            return question;
        }

        public String[] getAnswers() {
            return answers;
        }

        public String getCorrectAnswer() {
            return correctAnswer;
        }
    }
    
}

wow. you are good at java. I tried the code and what I did was I added lines where you put the questions and was able to add new questions very simply. There are some things in that which I dont quite understand. some of the code you posted I have never seen before. So thnx cus it means I can learn some new stuff.

Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?

Quick addition on that code. Where the answer is compared, a null pointer exception is possible if no answer is chosen when you click ok. To fix it, change to this line

if (group.getSelection()!=null && group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){

Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?

If you are using many classes from a package, it can be just fine to use .* in most cases. If however one of your class names appears in multiple packages that you import with .*, like say List which is present in java.awt and java.util then it will be ambiguous and the compiler won't like it.

Well, you can't quite work with a custom input dialog like that. There is a way to do it with an Object array for responses, but if you want radio buttons you need to create your own custom dialog. Just for the heck of it, I wrote up one way you could put this all together with JDialogs. Of course, you could probably do it a bit easier with a simple panel. Perhaps this will give you some ideas to work from (the simple BoxLayout is pretty awful)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.*;

public class QuizFourBib extends JDialog {
        List<Question> questions = new ArrayList<Question>();
        int score=0;
        ButtonGroup group = new ButtonGroup();
        JButton btnOk = new JButton("Ok");
        
        public QuizFourBib(){
            super();
            setModal(true);
            setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
            btnOk.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            
            questions.add(new Question("Who killed Goliath?", new String[]{"David", "Saul"}, "David"));
            questions.add(new Question("Where was Moses born?", new String[]{"Egypt","Samaria"}, "Egypt"));
        }
    
        public int startQuiz(){
            int score=0;
            for (Question q : questions){
                displayQuestion(q);
                if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){
                    score++;
                }
            }
            dispose();
            return score;
        }
        
        private void displayQuestion(Question q){
            getContentPane().removeAll();
            for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){
                group.remove((AbstractButton)buttonGroup.nextElement());
            }
            
            JLabel questionText = new JLabel(q.getQuestion());
            getContentPane().add(questionText);
            for (String answer : q.getAnswers()){
                JRadioButton radio = new JRadioButton(answer);
                radio.setActionCommand(answer);
                group.add(radio);
                getContentPane().add(radio);
            }
            getContentPane().add(btnOk);
            pack();
            setVisible(true);
        }
        
    public static void main(String args[]) {
        QuizFourBib quiz = new QuizFourBib();
        int score = quiz.startQuiz();
        
        JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 
    }
    
    class Question {
        String question="";
        String[] answers;
        String correctAnswer;
        
        public Question(String question, String[] possibleAnswers, String correctAnswer){
            this.question = question;
            this.answers = possibleAnswers;
            this.correctAnswer = correctAnswer;
        }

        public String getQuestion() {
            return question;
        }

        public String[] getAnswers() {
            return answers;
        }

        public String getCorrectAnswer() {
            return correctAnswer;
        }
    }
    
}

Could you comment on everything how it works? :) Cause this will be usefull as a rookie java learner, so I can teach how this code work :D

Could you comment on everything how it works? :) Cause this will be usefull as a rookie java learner, so I can teach how this code work :D

That example is self-explanatory(well written names of variables and methods, easy to follow). If you have problems to follow it you better ask specific question instead of making general query...

Well I don't understand the code at all :P So that's why I asked about comment the code like this :

public class QuizFourBib extends JDialog { //*This is a class, with a extended JDialog

If you do not understand code at all then perhaps you should start learning from beginning and leave this till the time you will be able to follow action flow of well written code...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.