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 (Retired) Creating the MVP Current Progress

Matthew Francis
Matthew Francis
6,967 Points

Newbie - I'm lost in the "for:each loop" part...need aid clarifying

public String getCurrentProgress() {
    String progress = "";
    for (char letter: mAnswer.toCharArray()) {
        char display = '-';
        if (mHits.indexOf(letter) >= 0)  {
            display = letter;
        }
        progress += display;
    }
    return progress;
}

what I dont get is how does char display = '-' manage to censor the word "treehouse (mAnswer)" with "-", Im assuming it's something related on how progress and progress+=display works?

Sam Deighton II
Sam Deighton II
24,296 Points

We start off with all letters being "-", so "---------"

when we make a guess, the if statement is used used as a True/False statement for each index

the if statement says if a correct letter(mHits) is found replace the variable display(our '-') with that letter(our guess)

the progress string will then display that letter instead of the dashes.

1 Answer

Zachary Betz
Zachary Betz
10,401 Points

Hi Matthew.

So display defaults to '-' (we are assuming the user has not guessed correctly).

The value of display is only changed if the user has guessed a correct letter. i.e. if the below condition evaluates to true.

if (mHits.indexOf(letter) >= 0)  {
    display = letter;
}

If true, the above code would assign the correctly guessed letter to display.

Matthew Francis
Matthew Francis
6,967 Points

So display defaults to '-' (we are assuming the user has not guessed correctly).

What I'm confused about is this part exactly! How does the computer know how many "display" it needs to be incremented to display ----------. From my understanding

public String getCurrentProgress() {
    String progress = ""; //Stores the "progress" value;
    for (char letter: mAnswer.toCharArray()) {
        char display = '-';
        if (mHits.indexOf(letter) >= 0)  {
            display = letter;
        }
        progress += display; //Increment "display" once; but how does the computer know how many times it should be incremented in order to make the value of treehouse into ----------?
    }
    return progress;//Returns the value of "progress";
}
Sam Deighton II
Sam Deighton II
24,296 Points

The computer already knows how long the word is because it knows the word(mAnswer). The function .toCharArray() seperates each letter into a spot in the array, and each of those spots has and an index(how far it is from the beginning of the word) The for:each loop runs until there is no more letters (the end of the mAnswer array and the last number in the index)

Matthew Francis
Matthew Francis
6,967 Points

The for:each loop runs until there is no more letters (the end of the mAnswer array and the last number in the index) @Sam Deighton II

I TOTALY FORGOT ABOUT THIS ASPECT, EVEYRTHING MAKES SENSE NOW! THANK YOU EVERYONE!