I assume you have a Hangman class that works like a model that does three things (relevant for this):
- Gives you a string with one - for each character of the word, to guess
- Gives you a string that shows correctly guessed characters in the correct position.
- Gives you a string that shows which characters were used
They all depend on the state of the model, which would be
- word
- guessed the characters
Based on this, I would say that you should have three methods that return strings, and in each of these methods you create a new instance of StringBuilder. Line string is separated from state to understand why I disagree with Computerish.
StringBuilder is a more efficient way to create strings and then just use concatenation, but it is easy to use. You start by creating an instance.
StringBuilder builder = new StringBuilder();
Then you create a string by adding strings or characters (or other things):
builder.append('-'); builder.append('w');
When you are done, you create an instance of String from StringBuilder:
String string = builder.toString();
and you get "-w", which is a rather boring example.
source share