Ok, I am typing up a program in which will play hangman with a user. 

I have done most of the program, but I have to let the program randomly choose a word from the list of words stored in your array. 

Afterwards, it should clear the screen and display a series of asterisks to represent the letters to be guessed. 

Then, display one asterisk for each letter in the chosen word. When displaying the asterisks, make sure you place a space between each asterisk to make it easier to read. See the technical notes below for information about clearing the screen.

Two lines below the asterisks, that should show the user the number of guesses left. The game begins with 8 guesses.

Next, I should allow the user to enter a letter. The user should be able to enter upper or lowercase letters. 

Compare the letter against the ones in the solution. If the letter is in the solution, it should replace the appropriate asterisk(s) with the guessed letter and then redraw the screen (clear the screen, then rewrite the information back to the screen). 

If the guessed letter is not in the solution, it should decrement the number of guesses left and redraw the scree

So, my question is how do I do the asterisks part?

Here's what I have so far,

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

public class Hangman {

	
	public static void main(String[] args) {
		
	Scanner kbd = new Scanner(System.in);
	
	String enter;
	Scanner inStream = null;	
	String filename = "dictionary.txt";
	
	System.out.print("This is a word guessing game. A word will be selected at random and kept hidden." + '\n' + "You will try to figure out the secret word by guessing letters which you think are in the" + '\n' + "word. You will guess one letter at a time. If the letter you guess is correct, the" + '\n' + "position(s) of the letter in the secret word will be shown. You will be allowed 8 wrong" + '\n' + "guesses. If you gues incorrectly 8 times, you lose the game. If you guess all of the" + '\n' + "letters in the word, you win." + '\n');
		
	System.out.println('\n' + "Press enter to continue");
	enter = kbd.next();
	
	try 
	{
		inStream = new Scanner (new File ("dictionary.txt"));
	}
		catch (Exception e)
		{
			System.out.println("Error opening the file " + "dictionary.txt");
			System.exit(0);
		}
		
	while (inStream.hasNextLine())	
	{
		String line = inStream.nextLine();
		System.out.println(line);
	}
	inStream.close();	
		
	final int WORDS_LENGTH = 15000;
	
	String [] words = new String [WORDS_LENGTH];
	
	for (int i = 0; i < WORDS_LENGTH; i++)
	{
		words[i] = inStream.nextLine();
	}
	
	int j = pickrandom(15000);
	

	}
	
	public static int pickrandom(int count)
	{
	        Random generator = new Random();
	        return generator.nextInt(count);
	}

}

Thanks

Dani AI

Generated

A simple, reliable pattern for the asterisks part is to keep the secret word as a String and maintain a parallel char[] mask filled with '*'. On each guess, compare the guessed letter to the solution in a case‑insensitive way and, for every match, write the original character into the same index of the mask. When printing the mask, join the mask characters with a single space so the display shows * * a * style output. As noted, StringBuilder is handy for building that spaced line, but a char[] is usually simplest for the replace-in-place logic.

A few problems visible in 's code should be fixed before hooking up the asterisk logic: the Scanner used to read the dictionary is consumed (and even closed) and then reused; either read lines into an ArrayList<String> and pick a random element from that list, or reopen the file. Avoid hardcoding WORDS_LENGTH — use the actual list size when calling nextInt. Also use nextLine() to pause for an Enter key rather than next() (which reads a token).

Example of the core mask/update/display (concept only — not duplicating posted main code):

char[] mask = new char[solution.length()];
Arrays.fill(mask, '*');

char guess = Character.toLowerCase(scanner.nextLine().trim().charAt(0));
boolean hit = false;
for (int i = 0; i < solution.length(); i++) {
  if (Character.toLowerCase(solution.charAt(i)) == guess) {
    mask[i] = solution.charAt(i);
    hit = true;
  }
}
StringBuilder out = new StringBuilder();
for (char c : mask) out.append(c).append(' ');
System.out.println(out.toString().trim());

Extra tips: handle repeated guesses with a Set<Character> (ignore duplicates), decrement "guesses left" only on new wrong guesses, and detect win by checking whether any '*' remains. For clearing/redrawing the screen, prefer reprinting the relevant lines (or use ANSI sequences when targeting terminals that support them) since Java has no cross-platform console-clear call.

Have you looked into the String Builder or String Buffer classes? That was the first thing that popped into my head when thinking of how to do it. It has add and replace methods that may come in handy.

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.