4

I am in a beginner Java class and for a project I need to count how many times a condition returns TRUE(correctGuess) or FALSE(incorrectGuess) with a loop inside of a loop. The problem that I'm having is that the variables being incremented within the inner loop do not hold their incremented value as the loop reiterates. Therefore, the outer while-loop's condition is never false. I'm really new to programming and I can't figure out the solution. Thank you in advance for your time with this silly question and if there are any questions I would be happy do a better explanation. The code looks like this:

    int incorrectGuess = 0;
    int correctGuess = 0;   

    while(incorrectGuess < 6 && correctGuess < WORD_LENGTH) {

        //Gets the users first guess
        System.out.print("Please guess a letter [A-Z]: ");
        letterGuessed = keyboard.nextLine();

        for (int i = 0; i < WORD_LENGTH; i++){
            char value = wordLetterArray[i];
            String letterArray_value = String.valueOf(value);

            if(letterGuessed.equals(letterArray_value)){
                ++correctGuess;
            }

            else
                System.out.println("Bad comparison!");  

            if(i == WORD_LENGTH)
                ++incorrectGuess;   

        }   
    }
7
  • No, those values will hold. What tells you that they're not holding? Commented Nov 13, 2013 at 3:14
  • 3
    This: if(i == WORD_LENGTH) inside a for loop with this exit condition:i < WORD_LENGTH will never ever ever (forever? foreverever? foreverever?) evaluate to true. Commented Nov 13, 2013 at 3:14
  • 1
    What exactly is incorrectGuess supposed to track? Note that since incorrectGuess is incremented inside a for loop with the condition i < WORD_LENGTH and also inside an if statement with the condition i==WORD_LENGTH it can never actually be incremented, since it's impossible for both conditions to be true simultaneously. Perhaps you meant i==WORD_LENGTH-1 for the if statement? Commented Nov 13, 2013 at 3:16
  • Sorry, the program is for a game of hangman. An "incorrect guess" is when the user guesses a letter that is not part of the word. Because it is a game of hangman, you only get 6 incorrect guesses (to get a hanged man you draw first the head(1), body(2), arms(3 and 4), and legs (5 and 6)). A correct guess then tracks the number of correct guesses so that when the user correctly guess all letters, the program will terminate instead of continuing to ask for another guess. EDIT: YES, i meant i==WORD_LENGTH-1. Commented Nov 13, 2013 at 3:21
  • Also, this if condition is wrong, if(letterGuessed.equals(letterArray_value)) letterGuessed is a String, letterArray_value is a char so they'll never equal. Commented Nov 13, 2013 at 3:23

1 Answer 1

1

Looks like you may need to redesign the whole algorithm, but I can tell you what your main issue is with this looping forever:

// Seems legit
while(incorrectGuess < 6 && correctGuess < WORD_LENGTH) {

// Still seems legit
for (int i = 0; i < WORD_LENGTH; i++)

    // Well, there's a problem, i will never be equal to word length 
    //because a condition of the for loop is i < WORD_LENGTH
    if(i == WORD_LENGTH)
        ++incorrectGuess;   

Again, I feel you need to redesign your whole algorithm, but if you want it to continue, just pull the incorrectGuess increment line out of the for loop. This will give you the intended result:

    for (int i = 0; i < WORD_LENGTH; i++){
      char value = wordLetterArray[i];
      String letterArray_value = String.valueOf(value);

      if(letterGuessed.equals(letterArray_value)){
            ++correctGuess;
      }
      else {
            System.out.println("Bad comparison!");  
      }
    }   

   incorrectGuess++;   
Sign up to request clarification or add additional context in comments.

4 Comments

no it won't if(letterGuessed.equals(letterArray_value)) letterGuessed is a string, letterArray_value is a char
Look closer. His intended result was if the for loop ends, increment incorrectGuess, regardless if it was a correct guess. correctGuess will still be incremented using the above. I don't think his algorithm is right, I was just trying to explain why his while loop isn't ending.
Thank you @CaptainSkyhawk I had a more complex algorithm, but I've been having so much trouble with this project that I decided to start all over an simplify. Unfortunately, I think you are completely correct, and my simplification has created more problems than it fixed. I'll rewrite it. *sobs in corner
@Taylor, at this rate, I might need something more like moonshine to make it through tonight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.