Question

I have been trying to test if my text field contains whatever is in my hashset but for some reason it is not working here is my code :

int WrongCounter = 0;
int RightCounter =1;
boolean isOver = false;
while(!isOver){
    question.setText("You have a "+RandomWord.length()+" letter word " + set);
    char input = scan.next().charAt(0);
    hangMan(WrongCounter);
    if(set.contains(ansField.getText().charAt(0))){
        set.remove(ansField.getText().charAt(0));
        right.setText("You are right");
        RightCounter++;
    }
    else if(!(set.contains(ansField.getText().charAt(0)))){

        right.setText("You are wrong!");
        WrongCounter++;
    }
    if(set.size()==0||WrongCounter ==8){
        isOver = true;
        hangMan(WrongCounter);
    }

here is the hangMan method

private void hangMan(int wrongCounter) {


        switch(wrongCounter){
        case 1:
            hangMan.setText("     -------|\n     |\n     |\n     |\n___|___");
            break;
        case 2 :
            hangMan.setText("     -------|\n     |\n     |\n     |\n___|___");

            break;
        case 3:
            hangMan.setText("     -------|\n     |            O\n     |\n     |\n___|___");
            break;
        case 4 : 
            hangMan.setText("     -------|\n     |            O\n     |           /\n     |\n___|___");
            break;
        case 5:
            hangMan.setText("     -------|\n     |            O\n     |           /|\n     |\n___|___");
            break;
        case 6 :
            hangMan.setText("     -------|\n     |            O\n     |           /|\\\n     |\n___|___");
            break;
        case 7 :
            hangMan.setText("     -------|\n     |            O\n     |           /|\\\n     |           /\n___|___");
            break;
        case 8 : 
            hangMan.setText("     -------|\n     |            O\n     |           /|\\\n     |           / \\n___|___");
            break;
        }

    }

Whenever I try this program it crashes and I have no Idea why

Was it helpful?

Solution

That is not a crash, that's an infinite loop. The program becomes unresponsive because you are probably executing this code inside an ActionListener attached to the JTextField or a JButton which is called by the EDT thread.

A GUI java application is not like a console application, there are multiple threads involved and you should not block the event thread with a loop like in your example. You have some unused code:

char input = scan.next().charAt(0);

which was blocking in your console version for input. Now you don't need a loop anymore because it's the user itself which will repeat the action on the user interface and call the actionPerformed method multiple times.

What do you want to do exactly? Verifying that the entered text is a permutation of the hidden word? Or just check the first character? In both situations you should remove the loop and store your variables outside the actionPerformed method:

int wrongCounter = 0;
int rightCounter = 0;

public void actionPerformed(ActionEvent e) {
  char currentCharacter = textField.getText().charAt(0);

  if (set.contains(currentCharacter)) {
    ++rightCounter;
    // whatever
  }
  else {
    ++wrongCounter;
  }

  if (set.size() == 0 || wrongCounter == 8)
    hangMan();  // doesn't need parameter anymore, it's outside the method
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top