Hey
can any one help me i'm doing a hangman program and want to display blanks where a to z is. how can i do this?

I've got it replacing "a" but can't get it to replace the rest i.e. b to z

any help would be appriaciated.

show = pickWord.replaceAll("a"," _ ");

Dani AI

Generated

Nice quick solve by — using a regex to mask letters is a compact way to get the initial blanks. For a working Hangman game it's usually better to keep the secret word intact and maintain a separate masked view you update on each guess. That preserves spaces and punctuation, handles case cleanly, and avoids repeatedly running global replacements.

Create the initial display by building a char array that replaces letters with underscores and leaves non-letters alone:

String secret = pickWord;
char[] masked = new char[secret.length()];
for (int i = 0; i < secret.length(); i++) {
    char c = secret.charAt(i);
    masked[i] = Character.isLetter(c) ? '_' : c;
}
String display = new String(masked);

When a player guesses a letter, scan the original word and reveal matching positions in the masked array:

char guess = Character.toLowerCase(inputChar);
for (int i = 0; i < secret.length(); i++) {
    if (Character.toLowerCase(secret.charAt(i)) == guess) {
        masked[i] = secret.charAt(i); // reveal with original case
    }
}

Notes and cautions: using String.replaceAll is fine for a quick one-off mask, but remember its first argument is a regex (see the String.replaceAll javadoc) and ASCII-only classes miss non-ASCII letters. Character.isLetter handles Unicode letters (see Character.isLetter). Keeping a char[] or StringBuilder for the mask makes incremental updates simple and efficient and keeps punctuation/spacing intact for multi-word phrases.

Thanks
Anyway i just figured it out. Its alwas the way. wrecking my head for ages and once i post it i figure it out myself.

show = pickWord.replaceAll("[a-z]"," _ ");
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.