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);