I am trying to write a program that will work like a hangman game. I have a loop that assigns the characters to their spot in the string and is supposed to end if the newly assigned string equals the original word.

If the letter guessed is correct, nothing should be added to incorrect(incorrect is the amount of incorrect guesses). If a wrong letter is guess incorrect should be increased by 1, even if the letter is guessed twice. After incorrect = 10 the player loses.

My problem is that it goes into an infinite loop. Incorrect never increases in value either. I have searched without finding an answer...but I believe it may have something to do with .equals because nothing inside these if statements with .equals when the strings are equal works.

Thank you for the help. Here is my code.

gameLoop:
                do{
                    //System.out.println("testing point");
                    Scanner sc = new Scanner(System.in);
                    char letter = sc.nextLine().charAt(0);
                    tempString = new StringBuilder(sBuilder);

                    if(copySecret.equals(sBuilder)){
                        break gameLoop;
                    }else{

                    for(i=0; i<numberOfCharacters; i++){
                        if(letter == secret.charAt(i)){
                            sBuilder.setCharAt(i, letter);
                        }
                    }
                    
                    if(tempString.equals(sBuilder)){
                        incorrect++;}
                    }
                    System.out.println(""+sBuilder);
                }while (incorrect < 10);

Recommended Answers

All 2 Replies

if(tempString.equals(sBuilder))

That line will not work since sBuilder is a String, while tempString is a StringBuilder. They are different types of objects. The equals() method is meant to test if two objects of the same type (i.e. two Strings, not one String and one StringBuilder) are equal. I imagine you are having a similar problem with your other equals statement, but I can't be sure since you didn't post enough code.

if(tempString.equals(sBuilder))

That line will not work since sBuilder is a String, while tempString is a StringBuilder. They are different types of objects. The equals() method is meant to test if two objects of the same type (i.e. two Strings, not one String and one StringBuilder) are equal. I imagine you are having a similar problem with your other equals statement, but I can't be sure since you didn't post enough code.

Thank you! This actually helped a lot, now I understand the separation between string and stringBuilder better. I fixed some of the parts with loops and nested if statements. This part of my code is working now.

Thank you very much for the help.

commented: No problem - if you have any more issues don't hesitate to ask. +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.