Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Current Progress

I don't understand how the code makes the dashes

I don't understand how when Craig runs the code it shows 6 dashes how does it get that?

3 Answers

Sandro Müller
Sandro Müller
1,089 Points

Well the question is old, but I stumbled over that question myself for a second. I will try to give a proper answer. First: He did not really explain that properly, so that this question occurs is no wonder. To understand this, you first have to grasp this concept:

Let's say I have a string like that <String name = "Sandro">. What happens if I apply "toCharArray" to that? Answer: Sandro.toCharArray will look like that: {'S', 'A', 'N', 'D', 'R', 'O'}.

If you understand this you can probably answer what will happen to this string: <String test = "hallo"> if I apply toCharArray.

If you are here you are getting closer to understand this. The next concept you have to understand is the advance for loop:

for( char x : Sandro.toCharArray()){

This for-Loop will go through my whole {'S', 'A', 'N', 'D', 'R', 'O'}-Array and the value x will takes all value of Sandro.toCharArray():

  1. x = 'S'
  2. x = 'A'
  3. x = 'N'
  4. x= 'D'
  5. x = 'R'
  6. x = 'O'

So maybe you noticed already that this advanced for-loops ends, after it goes through the whole {'S', 'A', 'N', 'D', 'R', 'O'}-Array, so through all 6 letters.

If you can answer me this question, you are only one step away, to understand the whole thing. What will be the value of <int counter> after the loop?

  1. int counter = 0;
  2. for( char x : Sandro.toCharArray()){
  3. counter = counter + 1;
  4. }

Well, we already noticed that this loop ends after is goes through the whole {'S', 'A', 'N', 'D', 'R', 'O'}-Array. Since x has to take all the values before it can end this loop, counter will have the value 6.

Now we come to the final part:

  1. String progress = "";
  2. for (char letter : Sandro.toCharArray()){
  3. char display = '-';
  4. progress +=display;
  5. }

This is exactly doing the same thing again, but this time we do not have a counter to that we add one each time x takes a different value. This time we add this sign: ' - ' to the <String progress>.

We initialize <String progress= " "> After going through the advanced for-loop the <String progress> will look like that: " - - - - - -" Maybe you can answer, why there are 6 of them.

Wait a second, this is not the code. The whole code looked like that, did not it?

  1. String progress = "";
  2. for (char letter : answer.toCharArray()){
  3. char display = '-';

  4. if(hits.indexOf(letter) !=-1){
  5. display = letter;
  6. }
  7. progress +=display;
  8. }

Well, yes you are right. Let us take a look what is different. Every time I run through the loop it will be compared to what we saved in <hits>. So what do we compare it to? Let us take a look at our code:

Programm start:

  1. prompter.displayProgress(); //displays the dashes
  2. Make a guess: 't' // t will be saved in hits, since t is part of the word "treehouse"
  3. prompter.displayProgress(); // now t will be displayed

Wait a second, does that not explain all our questions? I mean we call <display.Progress> even before we made a single guess. If we did not make a guess, nothing will be stored in hits and hits is just empty. Looking at our code once more, maybe it becomes clearer:

  1. String progress = "";
  2. for (char letter : answer.toCharArray()){
  3. char display = '-';

  4. if (hits.indexOf(letter) !=-1){
  5. display = letter;
  6. }
  7. progress +=display;
  8. }
  9. return progress;
  10. }

So we will never store a letter into display, because before making a guess <hits.indexOf(letter) !=-1)> will not be true. That means, writing <prompter.displayProgress()> int the beginning of our programm, will run the advanced for loop where the if-Statement can be ignored, hence only displays " - - - - - -".

If you understand everything completely, you maybe can answer the last question by yourself:

If I type 'e' you will get that: --ee----e. Why?

This was a great help! Thank you for taking the time to write this.

this was a BIG FAT help, thank you

gurveer aulakh
gurveer aulakh
2,800 Points

because we defined it by writing ' char display = "-"; ' btw it's in line 25 of game.java class.

First check hangman.java you see that you have written "treehouse" that goes to variable answer. So answer is made in string and we want to convert to "----", right? Because we want to see the result went you press 't' it shows "t-------".

first you have for each loop to convert string answer we call toCharArray that converts from string to char. Then we create a new variable that store every char called char letter.

out side the for we made variable display = '-';

so we call that display inside the for each loop that converts every letter to dash.

and inside the for each loop we do if else states: if you have answered correct one word, show up from the dash

display += letter; ..

hope you understand now.