Reputation: 21
I'm trying to get the convert the word from a txt file to a multiple "_" for example, let say I have to word dog I want it to print like _ _ _ so than the user can guess the letters I have tried this way but it didn't worked .
for(int i=0;i<randomWordLength;i++){
Integer.toString(i);
i=symbol;
System.out.println(i);
}
Upvotes: 1
Views: 108
Reputation: 159114
You can do it in a single line like this:
System.out.println(word.replaceAll(".", "_ ").trim());
The trim()
is there to remove the final trailing space. If the string is just printed like that, it's not visible anyway, so you can remove the trim()
call. It's included here for completeness.
Upvotes: 4
Reputation: 13859
This approach works with any word.
String wordToGuess = "Hello";
for (int i = 0; i < wordToGuess.length(); i++) {
System.out.print("_ ");
}
System.out.println();
Upvotes: 1