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 trialPeter Danglay
861 PointsI 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
1,089 PointsWell 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():
- x = 'S'
- x = 'A'
- x = 'N'
- x= 'D'
- x = 'R'
- 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?
- int counter = 0;
- for( char x : Sandro.toCharArray()){
- counter = counter + 1;
- }
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:
- String progress = "";
- for (char letter : Sandro.toCharArray()){
- char display = '-';
- progress +=display;
- }
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?
- String progress = "";
- for (char letter : answer.toCharArray()){
- char display = '-';
- if(hits.indexOf(letter) !=-1){
- display = letter;
- }
- progress +=display;
- }
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:
- prompter.displayProgress(); //displays the dashes
- Make a guess: 't' // t will be saved in hits, since t is part of the word "treehouse"
- 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:
- String progress = "";
- for (char letter : answer.toCharArray()){
- char display = '-';
- if (hits.indexOf(letter) !=-1){
- display = letter;
- }
- progress +=display;
- }
- return progress;
- }
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?
gurveer aulakh
2,800 Pointsbecause we defined it by writing ' char display = "-"; ' btw it's in line 25 of game.java class.
aroshinemunasinghe
5,649 PointsFirst 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.
Stephen McBride
7,769 PointsStephen McBride
7,769 PointsThis was a great help! Thank you for taking the time to write this.
Fatimah Anwar
Android Development Techdegree Student 10,416 PointsFatimah Anwar
Android Development Techdegree Student 10,416 Pointsthis was a BIG FAT help, thank you