0

I'm trying to make a java quiz linked to an sqlite database from where it's getting the informations (Questions and answers). Every quiz has n questions. A question has 4 options. A question can have more than one true question. so My database tables are: Quiz(id_quiz, quiz_name) Question (id_question, question, #id_quiz) Answer (id_answer, statut, answer, #id_question)

So I created a first frame to select the quiz that we want to pass, it's working with a function that's calling the new Frame depending on the selected quiz.

My problem is when I compare the selected answers with the true answer that must be selected. this is my code:

public boolean compare(List<Integer> trueAnswer, List<Integer> selected){

        if (trueAnswer.equals(selected)){
            return true;
        } else {
            return false;
        }

    }

The first step is to get the question in the jLabel, and the answers of that question too:

public List<Answer> showQuestion(int index){
in = fillQuestion(idQ).get(index).getIdQuestion();
jQuestion.setText(fillQuestion(idQ).get(index).getQuestion());
try {
        con = DriverManager.getConnection("jdbc:sqlite:myflightdb.db", "", "");
        String sql2 = ("select r.id_answer ,r.answer,  r.statut from question q, answer r where q.id_question = ? and r.id_question = q.id_question ;");
        PreparedStatement psmt2;
        psmt2 = con.prepareStatement(sql2);
        psmt2.setInt(1, in);
        List<Answer> listRep = new ArrayList<Answer>();
rs2 = psmt2.executeQuery();
        while(rs2.next()){
            int idR = rs2.getInt("id_answer");
            String rep = rs2.getString("answer");
            String statut = rs2.getString("statut");
            Reponse r = new Reponse(idR, rep, statut);
            listRep.add(r);
        }
return listRep;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return null;
        }

the function getTrueAnswer is made in order to get the list of true answers for question:

public List<Integer> getTrueAnswer(int index){
    int idquestion;
    idquestion = fillQuestion(idQ).get(index).getIdQuestion();
    List<Integer> rep = new ArrayList<Integer>();
    try {
    con = DriverManager.getConnection("jdbc:sqlite:/Users/gate11/Desktop/MyFlight/myflightdb.db", "", "");
    String sql = "select r.id_reponse from Reponse r where r.id_question = ? and r.statut like 'true';";
    PreparedStatement psmt = con.prepareStatement(sql);
    psmt = con.prepareStatement(sql);
    psmt.setInt(1, idquestion);
    rs = psmt.executeQuery();
    while (rs.next()){
        int idTrueAnswer= rs.getInt("id_reponse");
        rep.add(idTrueAnswer);
    }
    //JOptionPane.showMessageDialog(null, Arrays.toString(rep.toArray()), "Selected IDs", JOptionPane.INFORMATION_MESSAGE);

    return rep;
    } catch (Exception e){
        System.out.println(""+e.getMessage());
    }
    return null;
}

The button next must compare the selected jCheckBoxes with the true questions, if it's the same, the mark must be positif (so I can add the point to the result) or add 0 if the user doesn't choose the true one.

The problem is for the first time, it's working (I made the JOptionPanes to see the lists) so if I selected the first question, I have the selected is [1] and the true answer is [2] but in the second question, if I select 1 I have the list is [1,1] for the third one [1,1,1] the fourth [1,1,1,1] etc ... And I don't have any loop I can't find the problem. If there is any idea please don't hesitate. P.S: I did the 4 checkboxes because when I tried to add it in a for loop like that:

for (Answer a:listRep){
JcheckBox mycheck = new JcheckBox();
mycheck.setText(a.getQuestion());
mypanel.add(mycheck);
}
6
  • Could it be that you simply don't reset the list when you select a new question? Commented May 14, 2018 at 14:44
  • @munHunger I added selected.clear(); the list doesn't keep the selected from the last question, but makes doubles for the selected answers Commented May 14, 2018 at 14:46
  • And I assume that if you select 2 on the second run you will get the list [1,2]? Commented May 14, 2018 at 14:47
  • @munHunger no, it will get [2,2] ans for the third it will be [2,2,2] Commented May 14, 2018 at 14:50
  • Ah, then perhaps when you are selecting the new question you are not clearing the previous checkboxes from the panel and thus their actionlisteners are being triggered? That or you might have multiple actionlisteners on the checkboxes if you reuse them for each question? Commented May 14, 2018 at 14:53

1 Answer 1

1

I found the problem, the itemListener must be added to the jCheckBoxes in the initialisation of the components.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.