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 trialChris Rubio
1,643 PointsIf display already has a value of "-",how is it it/or I can change its value to letter. It doesnt make any sense ???
public String getCurrentProgress(){ String progress = ""; for(char letter : answer.toCharArray()){ char display = '-'; if(hits.indexOf(letter) != -1){ display = letter; } progress += display; } return progress; }
1 Answer
michaelcodes
5,604 PointsHi there! So when we initially are declaring the variable
char display = '-';
This declares a variable that holds a character, named display. We just put in a placeholder of "-" inside of the variable to be used by default. Because this is a variable though we can change its value at anytime. The next line down we could say
display = 'A';
And this would set the display variable to A.
In the context of this code, we are running a for loop to check each individual letter in the answer string to see if there is a match
for(char letter : answer.toCharArray()){
If there is a match, we are able to take the current letter (letter variable), and assign it directly to the display variable in order to show that letter.
Hope that helps, take care and happy coding !