Hey I am almost done with my hangman game for java but Im having a tough time in getting the last bit of it. I have included ALOT of comments for you guys to help me out with it.

import java.io.*;
import java.util.*;
import javax.swing.*;

public class Hangman 
	public static void main(String[] args) throws Exception 
		boolean[] wordsUsed = new boolean[50];
		int totalGuesses = 0;
		int wrongGuesses = 0;
		File wordFile = new File("F:\\randomwords.txt");
		Scanner input = new Scanner(wordFile);
		String[] list = new String[50];
		for(int i = 0; i <= list.length - 1; i++)
			list[i] = input.nextLine();
		// Picking a word at random
		int randomWordNo;
			randomWordNo = (int)(Math.random() * 50);
		while (wordsUsed[randomWordNo]);
		wordsUsed[randomWordNo] = true;
		String theWordToGuess = list[randomWordNo];
		// Create StringBuilder to hold the word as-guessed so far
		// That starts out as the word TO guess, and then we change
		// the letters into asterisks.  As the player guesses letters,
		// they'll change back from asterisks to letters, until 
		// wordSoFar again becomes the same as theWordToGuess, at 
		// which point the player has solved the puzzle.
		StringBuilder wordSoFar = new StringBuilder(theWordToGuess);
		for(int k = 0; k < wordSoFar.length(); k++)
			wordSoFar.setCharAt(k, '*');
		// Now we go into a loop, asking the user for letters.
		// If the letter is NOT in the string, it's a wrong guess,
		// and we go again.
		// If the letter IS in the string, then we change the characters
		// in wordSoFar from asterisks to the actual letter, and credit
		// the user with a correct guess.
		// we continue until wordSoFar matches theWordToGuess
			// Prompt the user for the next character to guess
			System.out.println("Guess a letter" );
			// Look at wordSoFar to see if the character they guessed is
			// already there.  If so, they've (rather stupidly) guessed
			// a character they've already successfully guessed.
			// If not, then look at theWordToGuess, and see if the letter
			// they just guessed is THERE.  If not, then count this as a
			// wrong guess and go again
			// If SO, then for all positions in wordSoFar, change the 
			// (current) asterisk to the character they just guessed,
			// if that character is in the same location in theWordToGuess
		} while (wordSoFar.indexOf(theWordToGuess) == 0);
		System.out.println("Congratulations you solved the problem in " + totalGuesses + " guesses, with " + wrongGuesses + " incorrect guesses");

One, what's the question?

Two, what's the point of the loop fro 25 - 28? Just pick one random number number from 0 to 49 and use that as the index. The array declared on line 9 seems pointless. You're reading in 50 Strings from the file and picking one at random. Why the boolean array?

Three, lines 57 - 76 - I see a prompt for user input, but I don't see anywhere where you actually read in any user input? Nor do I see anywhere where wordSoFar is ever searched for a letter, nor do I see anywhere where theWordToGuess is searched for that letter, nor anywhere where theWordToGuess is changed. Basically I see a bunch of comments that seem reasonable enough, but no matching code. Which brings us back to question one. What's the question? Is the question "Do my comments seem like a reasonable approach?" or is it "How do I implement my comments?" If the former, yes, the approach seems reasonable. If the latter, you need to set up a Scanner or whatever to read in user input, then do your searches for that user input, then replace wordSoFar with asterisks where needed. Basically what you have in the comments.

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