Stuck on my Java homework - Hangman game with StringBuilder - help?

* NOTE: I DIDN’T ASK YOU TO MAKE MY YOUTH. I'm just stuck.

I am developing the Hangman class now. Apparently, we need three StringBuilders (a) to display a hyphen: "--------" length of the word, b) one to display the correct letters that guessed: "--a--e---" and finally c) another that is essentially the opposite of b (guessed letters replaced by hyphens and made public letters). The goal of c) is to see if there are any matches during the guessing.

My biggest problem is that I cannot find many practical examples of StringBuilder on Google, namely, my biggest problem is where can I instantiate three StringBuilders in the Hangman class?

thanks...

+4
source share
4 answers

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.

+2
source

A brief example:

 StringBuilder helloWorldBuilder = new StringBuilder(); helloWorldBuilder.append("Hello"); helloWorldBuilder.append(" "); helloWorldBuilder.append("World"); helloWorldBuilder.append("!"); String helloWorld = helloWorldBuilder.toString(); 

And yes, you can create as many StringBuilder objects as possible (provided that you have an unlimited heap, but they practically say: "any number", which is more than 3 :-))

0
source

The purpose of StringBuilder is to build a string just before it is consumed. They must be created every time every time (this is good practice for the more complex problems that lie ahead). Therefore, instead of thinking about the stringbuilder class, think, not how to build the desired string. Assuming you store the correct guesses in a char array, you can have three methods that build the string as needed. In one of these cases, you don't need a StringBuilder at all.

Your ToBlankFormat method might look like this:

 public String toBlankFormat() { char[] format = new char[answer.length()]; Arrays.fill(format, '-'); return new String(format); } 

Essentially, to use StringBuilder all you do is add strings, characters, etc., and then call the ToString() method. An example would look like this:

 public String example() { StringBuilder builder = new StringBuilder("Hello"); // initial value if (addSpaces) { builder.append(" "); } builder.append("World"); return builder.ToString(); } 
0
source

Three StringBuilders must be instance variables of your class. You can create them in your class constructor, or you can create them when you select the word you want to guess.

-1
source

All Articles