I'm making a Hangman program to test my knowledge of GUI's. However my program is not checking letters correctly. This is my code for a Field Listener that checks the user's input with the word. I'm not posting all of the code because it is not all relevant to the issue and is working fine. The field listener is called after the user inputs text and presses the enter key. The object word.letters is a random String from a dictionary file. My problem has two parts: if the user inputs a correct letter the for loop never runs and the letter is instead counted as wrong in the else statement. My second problem is that if the user inputs the correct word it doesn't say it is correct instead it says it is an invalid guess. I hope this is enough.
The listener does handle invalid guesses correctly and wrong letters (but counts right letters as wrong).
class fieldListener implements ActionListener { @Override public void actionPerformed(ActionEvent ae) { if (guesses > 0) { String guess = field.getText(); //gets the text from field if (guess.length() == 1 && gc.contains(guess) == false && Arrays.asList(word.letters.toCharArray()).contains(guess)) { //if the guess is one letter long and is not found in the ArrayList gc containing guessed letters and word.letters contains the letter for (int i = 0; i < word.letters.length(); i++) { //for every letter in word.letters String str = ""; //this represents a JLabel that uses underscores for every letter of word.letters if (guess.equals(word.letters.charAt(i) + "")) { //if one letter is equal str += guess + " "; //change the underscore to the letter word.flags[i] = true; //set that letters flag to true meaning it has been found area.setText("You guessed a correct letter."); //print the result gc.addElement(guess); //add the letter to the found list list = new JList(gc); //refresh the JList field.setText(""); //empty the guess field } else if (word.flags[i] == true){ //otherwise if that letters flag is true str += word.letters.charAt(i) + " "; //add the letter to the label } else { str += "_ "; //otherwise add an underscore representing no found letter } t.setText(str); //set the label to the value of str } } else if (guess.equals(word.letters)) { win(); //if the guess equals the random word execute the win function list = new JList(gc); //update the JList field.setText(""); //empty field } else if (guess.length() > 1 && (guess.length() < word.letters.length() || guess.length() > word.letters.length())) { area.setText("Only guess letters or the entire word."); //if the guess is not one letter or the length of the word tell the user he made an invalid guess field.setText(""); //empty } else if (gc.contains(guess)) { //self explanatory area.setText("You already guessed that."); field.setText(""); } else { //if its a wrong letter or wrong word guesses--; //decrement the guesses gc.addElement(guess.toLowerCase()); //add the word/letter to the scroller area.setText("Wrong, guess another letter or the word."); //result list = new JList(gc); //update list f.repaint(); //repaint the frame to add the next body part field.setText(""); } if (!Arrays.asList(word.flags).contains(false)) { win(); //if there are no more false flags left execute win } } if (guesses == 0) { lose(); //if guesses = 0 execute lose } } }