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);
}
[1,2]?