-3

I am trying to create a swing app that is a quiz. I need the jLabel to change on a button click but when I click the button, the app locks up. Can someone point me in the right direction?

My button click code is below:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String[] questions = {"test0","test1","test2","test3","test4","test5","test6"};
    String[] answers = {"","","","","","",""};
    int i = 0;

    do {
        jLabel2.setText(questions[i]);
        index.setText(String.valueOf(i));
        if (txtAnswer.getText().toLowerCase().equals(answers[i].toLowerCase())) {
            i++;
            jLabel2.setText(questions[i]);
        }
        else {
            add(lblWrong);
        }
    }
    while(i < 7);
}      

I am getting a warning that the evt parameter has not been used, could this be a problem?

Thank you

takendarkk
  • 3,347
  • 8
  • 25
  • 37
pls3399
  • 53
  • 6

2 Answers2

1

In the else condition of your loop, you don't add 1 to i at all - therefore you can potentially end up in the situation where it's never incremented, thus it will be an infinite loop (locking your program up.)

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • I don't want them moving on until they get it right. Shouldn't I only increment it in the if statement? – pls3399 Jul 02 '14 at 15:55
1

You don't want the do while loop. It's trapping you in the button press method as if you get an answer wrong you keep entering the else and can't leave it, stopping the app from working. Replace it with an if statement checking if i < 7.

Woodi_333
  • 479
  • 2
  • 7