Jellyman Jellyman - 4 days ago 5
Java Question

Adding a space in between underscores

I'm trying to make a hangman game right now and a requirement is to use underscores to display the letters.

So far for my code I have this:

char[] guessWord = new char[word.length()];
for (int i = 0; i < guessWord.length; i++)
guessWord[i] = '_';
System.out.println(guessWord);


the output looks like this:
_____
, but I want it to make it look like this:
_ _ _ _ _


I'm not sure how to insert that space in between. Any advice?

Answer

Just print the space in your for loop

char[] guessWord = new char[word.length()];
for (int i = 0; i < guessWord.length; i++){
    guessWord[i] = '_';
    System.out.print(guessWord[i] + " ");
}
System.out.println();
Comments