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

Kshatriiya .
Kshatriiya .
1,464 Points

How does String progress in getCurrentProgress() method knows how many dashes to store and which dashes to replace?

Hello, in the code below

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

  }

at the beginning of the game it displays -------- (each dashes for the answer "treehouse"), but how does the String variable progress knows how many dashes there are as well as which ones to replace with the right character when a hit is achieved? It even knows to replace several dashes when the right repeating characters are input (such as e). Could you perhaps explain to me how the value is passed down the code chain onto the variable progress?

I'm trying to sort of visualize the code and what each declared variable stores in each stage of the code and subsequently pass the value down in hierarchy. (for reference mAnswer stores the String "treehouse")

I'd imagine that this bit of code:

public String getCurrentProgress() {
    String progress = "";
    for (char lettert: mAnswer.toCharArray()) {
         char display = '-';

         }
         progress += display;
    }
         return progress;

  }

would give progress eight dashes because for each letter in mAnswer it would add a dash to progress. However, wouldn't the code just add the correctly guessed character to the progress alongside the dashes like this: --------treehouse, instead of replacing the dashes? What is causing the replacement?

Thanks in advance

1 Answer

Craig's explanation starts at the 2:34 time stamp, and sometimes it's easier to understand the transcript than the video. But to paraphrase Craig, the for loop will take each letter in the answer, and see if it is in mHits. If not it will make display a hypen (char display = '-'). But if the letter is in mHits (mHits.indexOf(letter) >= 0), then it will set display to that letter (display = letter) instead. Either way, display will be appended to progress.

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

  }

For example, suppose mAnswer is treehouse, and the user has guessed t, a and e. Then t and e will be in **mHits, and when **displayProgress()* is called, the loop will first check to see if t is in mHits, and since it is, it will append t to display, and progress with be t. Then the loop will check r, the next letter in mAnswer, against mHits, not find it, leave display -, append that to progress, and progress will be t-. Then the loop will check e against mHits, find it, make display e, and append that to progress, so progress will be t-e, etc. Once it has looped through all the letters in mAnswer (treehouse) it will return the final version of progress which, in this case, would be t-ee-----, since t and e are in mHits and a isn't.

The easiest way to see what's happening is to use the debug mode of your IDE and step through the code, watching the values of mAnswer, mHits, display and progress.

Kshatriiya .
Kshatriiya .
1,464 Points

Yeah it does make a lot of sense now thanks. It took me a while to realize that the code runs 8 times through each character in "treehouse" for each iteration of the game.

Thanks for your explanation :)