Search code examples
javasubstringindexofstring-length

Why does my java guessing game not work after the first loop


I'm working on this guessing game where the user needs to guess the word in under 6 tries. They have the ability to try and guess the whole word but if guessed incorrectly the game ends. When the game ends it gives them the option to play again. My problem is that when I try to guess the word for the second time it gives me an error only when I enter n character.

I'm later going to replace add an array instead of the static BRAIN word and randomize it but I want to figure this out.

Here is the code:

/*
* WordGuess.java

*/
import java.util.Scanner;

/**        
* Plays a word guessing game with one player.
* */

public class WordGuess {

//Main meathod    
public static void main(String[] arqs) {

final String SECRET_WORD = "BRAIN";
final String FLAG = "!";
String wordSoFar = "", updatedWord = "";
String letterGuess, wordGuess = "";
int numGuesses = 0;
String repeat = "";

Scanner input = new Scanner(System.in);

/* begin game */
System. out. println ( "World Guess game. \n" );

do{
numGuesses = 0;
wordSoFar = "";
updatedWord = "";

for (int i = 0; i < SECRET_WORD.length() ; i++) {
wordSoFar += "-"; //word, as dashes
}
 System.out.println("Your word is  ");
System.out.println(wordSoFar + "\n"); //displays dashes
/* a11ow player to make guesses */

do{
System.out.print("Enter a letter (" + FLAG + " to guess entire world): ");
letterGuess = input.nextLine();
letterGuess = letterGuess.toUpperCase() ;
/* increment number of guesses */
//numGuesses += 1;
/* player correctly guessed a letter--excract string in wordSoFar
* up to the letter guessed and then append guessed. letter to that
* string Next, extract rest of wordSoFar and append after the guessed
* letter
*/

if (SECRET_WORD.indexOf(letterGuess) >= 0) {
updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess));
updatedWord += letterGuess;
updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1,
wordSoFar. length() ) ;
wordSoFar = updatedWord;
}else {
    numGuesses += 1;
}
/* display guessed letter instead of dash */
System.out.println(wordSoFar + "\n");
} while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD) && numGuesses < 6);

/* finish game anil display message anil number of guesses */
if (letterGuess.equals(FLAG)) {
System.out.println("What is your guess? ");
wordGuess = input.nextLine() ;
wordGuess = wordGuess.toUpperCase() ;
}
if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD)) {
System.out.println ( "You won! " ) ;
} else {
System.out.println("Sorry. You 1ose.");
}
System.out.println("The secret word is " + SECRET_WORD);
System.out.println("You made " + numGuesses + " mistake.");

    System.out.println("Would you like to play again?");
    repeat = input.next();

}while(repeat.equalsIgnoreCase("Y"));

System.out.println("GOOD BYE THANKS FOR PLAYING!");
}

}//end of Word Guess class

Solution

  • Solution 1 - You can move Scanner input = new Scanner(System.in); to the first do block

      do {
    
            //Has moved to the first do block
            Scanner input = new Scanner(System.in);          
    
            //Rest of your code
    
          }
    

    Solution 2 - You can use input.next() insted of input.nextLine() in the second do block

    do {
    
        System.out.print("Enter a letter (" + FLAG + " to guess entire world): ");
    
        // input.nextLine() Has changed to input.next()
        letterGuess = input.next();
    
        //.. Rest of code
    
        }