I am doing a project in class for computer science. I have to make a hangman game that consists of the following: Get the words from a file and pull in the next word after each game, display the word as dashes for the user to guess, prompt the user to guess a letter, they get 6 incorrect guesses, gather all the letters the user guess and display them throughout the game, replace the correct guess letters with the dashes, then lastly display if they won or lose and ask if the user wants to play again.

I got stuck at the part of the dashes, the dashes are not coming up the correct amount of letters in the word it keeps showing one dash, then the rest I do not know how to do can someone please help?

import java.io.File;
import java.io.FileNotFoundException;

import java.util.Scanner;
import java.util.regex.Pattern;

public class Lab3 {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {

		//creating scanners
		Scanner input = new Scanner(System.in);
		Scanner game = new Scanner(new File("c:/words.txt"));
		String wordBlanks = " ";
		
	//welcoming user and giving discription of the game
		System.out.println("Welcome to a game of Hangman!");
		System.out.println("Dir ections: Try to guess the word one letter at a time. "
						+ "\r  \t    if you get 6 wrong letters game is over and you have the choice to play again.");
	
		
		//making a while loop to display each word one game at a time like "_ _ _ ..."
	
		while (game.hasNext()){
			String word = game.next();
		
			System.out.println();
			System.out.println("Let's begin...");
			
			//conducting for loop for each word being played with
			for (int i = 0; i < word.length(); i++){
				
				System.out.print("_ ");
				
			
		//prompting user to guess a letter
			System.out.println();
			System.out.println("Guess a Letter: ");
			
			//checking each guess with the letters inside the word	
			char guess = input.next().charAt(i);
			if (guess == word.charAt(i)) {
			
				System.out.println();
			}
				else if (guess != word.charAt(i)) {
					for (int j = 0; j < 6; j++){
						

					}
				}
			}	
		}
	}
}

Try this: you've got a curly brace missing after your printing of the underscore, either put one there or remove the one after the for loop right before it. Other than that, it looks good so far :)

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.