Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Current Progress

Chris Rubio
Chris Rubio
1,643 Points

If 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

5,604 Points

Hi 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 !