0

Hi! I'm having trouble solving this problem. Since it's finals week here at my school the Computer science tutors left early and I need just this simple problem finished by tomorrow @ noon.

I'm working on the hangman game as my final project. I've got everything done, except for this silly thing. I'm trying to detect if a user entered a letter already, and if they did, loop the input until they enter a letter that's not in this string array. Everything is working except that I can't exit this loop! Here's my code:

while (contains == true) {
            if (contains = true) {
              userInput = JOptionPane.showInputDialog(null,"This letter has already been entered! Please enter a letter");
              c = userInput.charAt(0);
              b++;
              
              for (int u=0;u<lettersEntered.length;u++) {
                if (lettersEntered[u] == c) {
                  contains = true;
                }
              }
            }
            else {
              lettersEntered[g]=c;
              System.out.println(lettersEntered[g]);
              contains = false;
            }
          }

Could someone please help my find my problem?! I don't know why it's not exiting the while loop. Whenever the boolean contains is false, it's supposed to exit the loop, but it's not. Someone please help! Thanks!

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by ddanbe
0

What's the difference between = and ==
Where are you using one, where you should be using the other?

0

What's the difference between = and ==
Where are you using one, where you should be using the other?

Thanks for replying,

Well, from my CS tutors, the difference is == is that it's comparing the boolean to see if it's true, while just an = is setting the boolean. So in the while/if/for loops, I want to compare to see if it's true, not set it.

Btw I've been just completely trying to get this answer right but after a few reviews, I've concluded that my logic is completely off (the while loop is only comparing the one element in the boolean array)... sad thing is I don't know where to start. Could I get some help on garnering ideas? I completely didn't come here for quick answers on this. I just don't know where to start, and some ideas could help. My idea for this loop was to do the following:

-Check lettersEntered array to see if letter is in array
-If it isn't, add that letter into the array
-if it is, prompt user to enter another letter
-keep prompting until letter is not found in loop
-set a boolean condition to false, which will exit the loop

But my logic is not working, and my compiler (stupid DrJava) isn't doing these steps.

0

OK, let me be blunt.
There's one place where you're using = where you should be using ==

You use == correctly in a similar place.

0

OK, let me be blunt.
There's one place where you're using = where you should be using ==

You use == correctly in a similar place.

Here's my updated (but still not working) code:

boolean contains = false;
          for (int we=0;we<lettersEntered.length;we++){
            if (lettersEntered[we] == c){
              contains = true;
            }
            else {
              lettersEntered[g]=c;
            }
          }
while (contains == true) {
            userInput = JOptionPane.showInputDialog(null,"This letter has already been entered! Please enter a letter");
            c = userInput.charAt(0);
            
            for (int u=0;u<lettersEntered.length;u++) {
              if (lettersEntered[u] != c) {
                lettersEntered[g]=c;
                contains = false;
              }
            }
          }

The problem is, I can't find a way to set contains to false or true only one time... I want to loop through the lettersEntered array to find out if any letter has already been entered. The problem is with the lettersEntered array..

0

The problem is that you are mixing two booleans.
First: make a boolean function to check if a letter is in an array or not.
Second: decide with a boolean when you want to end your while loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.