I need to read a text document (with a word per line on it) and extract a word from it to use in a game of hangman. I've been trying to do this for a while, but all I can get is the program to print all of the words in the text file in the console. I think I'm starting to confuse myself. Also, I need to create a class for the player in the game in which they type in their name and it records the number of wins and loses that they have, and for some reason I cannot get my program to do that. Anyway here is the code that I have so far:

import java.util.*;
import java.io.*;


public class hangman
{
	public static void main(String [] args) throws IOException
	{
		int maxTries = 7;
		int secretWordLen;
		
		
		Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
		String wordlist = fileScan.next();
		String secretWord [] = wordlist[(int)Math.floor(Math.random() * wordlist.length)];
		secretWordLen = Math.min(s.length(), maxWordLen);
		
        for (i=0; i<secretWordLen; i++)
        {
            secretWord[i] = s.charAt(i);
        }
		
		System.out.println("Welcome to the game of HANGMAN!!!!");
		System.out.println("You will have 7 chances to guess what the word is.");
		
		Scanner inScan = new Scanner(System.in);
		System.out.print("Please Enter Your name: ");
		String name = inScan.next();
		
		hangPlayer = new player(name);
		System.out.println(hangPlayer);
		
		
		
	
	}
}

private class player
{
	private player(String name)
	{
		int winCount = 0;
		int loseCount = 0;
		
		System.out.print(name + (", you have won " + winCount + " and you have lost ") + loseCount);
		return name;
	}
}

THANKS

Use a StringTokenizer(). It's default pattern is the space character, so it reads in only the words. You can loop through in a while loop.

I figured out a way to transfer a word from a text file to a String and StringBuffer but now I'm stuck with changing the letters in the stringBuffer to "_ _" so that I can display the number of letters in the word to the user. Does anyone have any suggetions?

import java.util.*;
import java.io.*;


public class hangman
{
	public static void main(String [] args) throws IOException
	{
		int maxTries = 7;
		int wordLength;
		int guess;
		
		//the fileScan gets the first word for the game
		Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
		String secretWord = fileScan.next();
		
		//Creates a StringBuffer for the viewing of the letters guessed
		StringBuffer letter = new StringBuffer(secretWord);
		System.out.println(letter);
		letter.length();
		
		
		System.out.println("Welcome to the game of HANGMAN!!!!");
		System.out.println("You will have 7 chances to guess what the word is.");
		
		System.out.println("Your word is");
	
	}
}

THANKS

Oh, so this is a console game.... In that case I would get the length of the current letter, print out that number of "_" and whenever a user types in a letter, you compare it (Strings, use indexOf() to get the position) to the real word. Find the position of the letter, and print out the "_" except have a letter in the correct position of the "_".

Sorry if that sounds confusing.

I cant figure out how to change a "_" to the letter guessed if its valid still. And I can't figure out where to put the code to make the game case insensitive. Any suggestions will help.

thanks again

import java.util.*;
import java.io.*;


public class hangman
{
	public static void main(String [] args) throws IOException
	{
		
		//	public static final Comparator<secretWord> CASE_INSENSITIVE_ORDER;
		
		int maxTries = 7;
		int wordLength;
		boolean solved;
		StringBuffer guessedLetters = new StringBuffer();
		
		//the fileScan gets the first word for the game
		Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
		String secretWord = fileScan.next();
		
		//Creates a StringBuffer for the viewing of the letters guessed
		StringBuffer word = new StringBuffer();
		for(int i = 0; i <= secretWord.length(); i++)
		word.append("_");
		System.out.println(word);
		
		
		System.out.println("Welcome to the game of HANGMAN!!!!");
		System.out.println("You will have 7 chances to guess what the word is.");
		
	
			
			
			
		//	System.out.println("Your word is " + wordLength + " letters long.");
			
	
			String letter;
			while(maxTries > 0)
			{
			System.out.println("The letters that you have guessed are: " + guessedLetters);
			System.out.print("Please enter a letter to guess: ");
			Scanner inScan = new Scanner(System.in);
			letter = inScan.next();
			guessedLetters.append(letter + " ");	
			
			if(secretWord.indexOf(letter) != (-1))
			{
				 secretWord.indexOf(letter);
				 System.out.println("correct");
			}
			else
				maxTries--;
			System.out.println("You have " + maxTries + " wrong guesses left.");
			}
			
		
	
	}
}

Mooky, how did you go finishing this Hangman project?
If you have a working completed one would u be able to send it to me at SNIPPED. I have an assignment due that very closely resembles your hangman and am in need of help

Mooky, how did you go finishing this Hangman project?
If you have a working completed one would u be able to send it to me at SNIPPED. I have an assignment due that very closely resembles your hangman and am in need of help

If you look very carefully, the thread you managed to dig from the grave is about 3 years old... =/

Every time someone revives an ancient old thread, a young kitten dies...

Save the kittens! O_O

i realize that, desperate times call for desperate measures and this blokes hangman is almost exactly what i need.

This article has been dead for over six months. Start a new discussion instead.