What my problem is I don't know how do I set it up so that when the entire word has been guessed I can stop the game and tell the user they won or lost.
I tried adding in another if statement after maxTries--;
where it says
if (maxTries == 0) {
println("You lose.")
but the message doesn't show up.

Other problem is that the letters being guessed are not being recorded properly, I thought it was fine but after testing it a few times the letters I've guessed would disappear from the display making the user retype them. And the while loop doesn't stop after finishing my guesses or when the guesses run out.

I could use some advice on this, and I'm just being honest this is for my cs class.

Thanks in advance!

public static void main(String[] args) throws FileNotFoundException {
	Scanner input = new Scanner(System.in);
	//do while loop that has worked great before with lab2
	boolean option = true;	
	String choice;
	do{
	
	//display welcome + instructions
	Welcome();
	
	//display everything else
	everything();
	
	//repeats the entire program if user presses y or ends it if presses n
	System.out.println("Play again? (y/n)");
	choice = input.nextLine();
	if (choice.equals("y")){
		option = true;
	}
	if (choice.equals("n")){
		option = false;
		System.out.println("Goodbye!");
	}
	}while (option == true); 

}
	public static void Welcome() {
		System.out.println("Welcome to a game of Hangman!");
		//welcome message and simple instructions. 
		System.out.println("Instructions are simple. Guess what the word is, you have 6 guesses. Use lower case letters only" + "\rand do not repeat the same letters. Once all guesses are used unsuccesfully then you lose!\n\n\n");	
}
	public static void everything() throws FileNotFoundException{
		//create scanners
		Scanner filereader = new Scanner (new File("C:/CS/wordFile.txt")); 
		Scanner input = new Scanner(System.in);
		
		//word is now next word in .txt
		String word = filereader.next();
	//make while loop to count down the loss of the user 
	int maxTries = 6; 
	while (maxTries > 0) {	
		//build is now the length of whatever word is.
		StringBuilder build = new StringBuilder(word.length());
		
		//sets up word to print out in underscores
		for(int i = 0; i < word.length(); i++) {
			build.append("_");
		}
		
		//set char array
		char[] wordArray = word.toCharArray(); 
		
		//count letters in word
		int charCount = word.length();
		
		//while loop to count words greater than 0 and goes on from there to 
		while(charCount >= 0){
		
		//that build being turned into a string
		System.out.println(build.toString());
			
		//ask user for letter
		System.out.println("Enter letter: ");	
		
		//store user input as a char
		char guessChar = input.next().toCharArray()[0];
		boolean foundletter = false;
		for(int i = 0; i < word.length(); i++)
		//comparing user input with the wordArray
			if(guessChar == wordArray[i])	{	
			//changing the underscore with the correct word 
			build.setCharAt(i, guessChar);
			foundletter = true;

			}
	//unsuccessfully print out letters tried.
	charCount--;
	System.out.println("Letters tried: " + guessChar);	
	
	//subtract number of maxTries for every incorrect guess
	if (foundletter == false){
		maxTries--;
		if (maxTries == 0){
			System.out.println("You Lose.");
		}
	}
	
	System.out.println("You have "  + maxTries + " " + "wrong guesses left");
		
	}
	}	
}
}

Recommended Answers

All 4 Replies

Made a few changes.

public static void main(String[] args) throws FileNotFoundException {
		Scanner input = new Scanner(System.in);
		//do while loop that has worked great before with lab2
		boolean option = true;	
		String choice;
		do{
		
		//display welcome + instructions
		Welcome();
		
		//display everything else
		everything();
		
		//repeats the entire program if user presses y or ends it if presses n
		System.out.println("Play again? (y/n)");
		choice = input.nextLine();
		option = (choice.equalsIgnoreCase("y"));
			if (!option) {
			System.out.println("Goodbye!");
		}
		}while (option == true); 

	}//end of main
	
		public static void Welcome() {
			System.out.println("Welcome to a game of Hangman!");
			//welcome message and simple instructions. 
			System.out.println("Instructions are simple. Guess what the word is, you have 6 guesses. Use lower case letters only" + "\rand do not repeat the same letters. Once all guesses are used unsuccesfully then you lose!\n\n\n");	
	
		}//end of welcome	
		
		
		
		public static void everything() throws FileNotFoundException{
			//create scanners
			Scanner filereader = new Scanner (new File("C:/CS/wordFile.txt")); 
			Scanner input = new Scanner(System.in);
			
			//word is now next word in .txt
			String word = filereader.next();
		//make while loop to count down the loss of the user 
		int maxTries = 6; 
		while (maxTries > 0) {	
			//build is now the length of whatever word is.
			StringBuilder build = new StringBuilder(word.length());
			
			//sets up word to print out in underscores
			for(int i = 0; i < word.length(); i++) {
				build.append("_");
			}
			
			char[] wordArray = word.toCharArray(); 
			
			int charCount = word.length();
			
			//while loop to count words greater than 0 and goes on from there to 
			while(charCount > 0){
			
			System.out.println(build.toString());
				
			System.out.println("Enter letter: ");	
			
			//store user input as a char
			char guessChar = input.next().toCharArray()[0];
			boolean foundletter = false;
			
			for(int i = 0; i < word.length(); i++)
			//comparing user input with the wordArray
				if(guessChar == wordArray[i])	{	
			
				//changing the underscore with the correct word 
				build.setCharAt(i, guessChar);
				

				}
		//get other results 
		charCount--;
		System.out.println("Letters tried: " + guessChar);	
		
		//subtract number of maxTries for every incorrect guess
		if (!foundletter){
			maxTries--;
			if (maxTries == 0){
				System.out.println("You Lose.");
			}
		}
		
		System.out.println("You have "  + maxTries + " " + "wrong guesses left");
			
		}
		}	
	}//end of everything
	}//end of entire program

Your nested loops are confusing you. What you have looks like this:

String word = { Get the next word. };
		// make while loop to count down the loss of the user
		int maxTries = 6;
		while (maxTries > 0) {
			{ Clear out guesses. Start guesses for this word. }

			// Loop once for each letter in the word, plus one extra:
			int charCount = word.length();
			while (charCount >= 0) {
				{ Get the user's guess. }
				charCount--;
				if ( {User guessed wrong} ) {
					maxTries--;
				}
			}
		}

When the "charCount" loop exits, the system throws away all of their successful guesses. (...as long as they have some tries left.)

Given one word to guess, why are there two loops instead of one? When thinking about how to design the looping, focus on this: Under what conditions do you want to exit the loop? IE: If the user runs out of incorrect guesses (6 of 'em), exit the loop. They loose. Also, if the user has the whole word, exit the loop. They won.

You might want to use the 'break;' statement to exit a loop.

Solved thanks JeffGrigg

P.S. Maybe the 'System.out.println("Letters tried: " + guessChar);' should print a String (or StringBuilder) value, not just the most recently guessed letter. :-/

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.