Problems when adding numbers to an Arraylist

Asked

Viewed 89 times

0

My code is 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 don’t want you to repeat questions. I’m trying to save the numbers generated in an Arraylist to check if a number has already left or not. However he is not adding the elements in the list, he is starting a new one every time he calls the method.

Instead of [1][2][3] he is:
[1]
[2]
[3]

In the Quiz class (where the ArrayList) is 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 still worked. I can’t find the problem at all =(

2 answers

1

You are creating a random without seed, and always creates a new Random() soon, it will always generate the same number

Place

new Random(System.nanoTime())

This way he is taking the nanoseconds in which the method is executed, which will prospecte that it is not always the same number even you of new Random in the method;

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

  • It didn’t work. The numbers are being generated correctly and alternately. Problem is when to put this number generated in Arraylist. It adds in the array, after it executes the method again, rather than in the next position, it "resets" the array by placing it back in the first position.

  • You can put a constructor with a log in the Quiz class to make sure it is being instantiated only once?

0


Is that all the code? If it is change:

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

    inicio.handleIniciar(event);

}

for:

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

    carregaInfo();

}

And not to create a new instance of Myconnection replace:

MyConnection myConnection = new MyConnection();

for

myConnection = new MyConnection();

and create a variable:

MyConnection myConnection;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.