Problems adding numbers to an ArrayList

0

My code looks like this:

public class QuestionarioController implements Initializable {

    InicioController inicio = new InicioController();
    Quiz questionario = new Quiz();

    @FXML
    private Label lblPergunta;
    @FXML
    private RadioButton rbQuestion1;
    @FXML
    private RadioButton rbQuestion2;
    @FXML
    private RadioButton rbQuestion3;
    @FXML
    private RadioButton rbQuestion4;
    @FXML
    private Button btnavancar;

    @FXML
    private void handleAvancar(ActionEvent event) throws IOException {

        inicio.handleIniciar(event);

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            carregaInfo();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QuestionarioController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(QuestionarioController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void carregaInfo() throws ClassNotFoundException, SQLException {
        MyConnection myConnection = new MyConnection();
        myConnection.statement = myConnection.connection.createStatement();
        Random gerador = new Random();
        int numeroGerado = gerador.nextInt(4);
        if (questionario.getNumeros().contains(numeroGerado)) {
            numeroGerado = gerador.nextInt(4);
        } else {
            questionario.setNumeros(numeroGerado);
        }

        String sql = "SELECT * FROM question WHERE cod_question = " + numeroGerado;
        myConnection.result = myConnection.statement.executeQuery(sql);
        while (myConnection.result.next()) {
            questionario.setDescription(myConnection.result.getString(2));
            questionario.setQuestion1(myConnection.result.getString(3));
            questionario.setQuestion2(myConnection.result.getString(4));
            questionario.setQuestion3(myConnection.result.getString(5));
            questionario.setQuestion4(myConnection.result.getString(6));
            //correct = myConnection.result.getString(7);

        }

        this.lblPergunta.setText(questionario.getDescription());
        this.rbQuestion1.setText(questionario.getQuestion1());
        this.rbQuestion2.setText(questionario.getQuestion2());
        this.rbQuestion3.setText(questionario.getQuestion3());
        this.rbQuestion4.setText(questionario.getQuestion4());
    }

}

The goal is that when I click Next, the same screen appears, only with different questions. So far so good, but I do not want you to repeat questions. I am trying to save the generated numbers in an ArrayList to go checking if a number has already left or not. However, it is not adding the elements to the list, it is starting a new one every time it calls the method.

Instead of [1] [2] [3] it is: Home [1]
[2]
[3]

In the Quiz class (where ArrayList is) it looks like this:

public class Quiz {

    private String description;
    private String question1;
    private String question2;
    private String question3;
    private String question4;
    private String correct;
    private int qtdAcerto;
    private boolean controle = false;
    ArrayList<Integer> numeros = new ArrayList();

    public ArrayList<Integer> getNumeros() {
        return numeros;
    }

    public void setNumeros(Integer numeros) {
        this.numeros.add(numeros);
    }

}

Other getters and setters were hidden.

I have tried to leave the list in this same control class, and it worked anyway. I can not find the problem in any way = (

    
asked by anonymous 26.10.2016 / 17:36

2 answers

0

Is this the whole code? If it is altered:

@FXML
private void handleAvancar(ActionEvent event) throws IOException {

    inicio.handleIniciar(event);

}

by:

@FXML
private void handleAvancar(ActionEvent event) throws IOException {

    carregaInfo();

}

And to not be creating a new instance of MyConnection replace:

MyConnection myConnection = new MyConnection();

by

myConnection = new MyConnection();

and create a variable:

MyConnection myConnection;
    
28.10.2016 / 21:34
1

You are creating random without seed , and always create new Random() so it will always generate the same number

Place

new Random(System.nanoTime())

In this way it is taking the nanoseconds in which the method is executed, which will guarantee that it is not always the same number even if you of new Random in the method;

Another way is to get the random from the method scope, and always use only the nextInt in the method.

    
26.10.2016 / 19:35