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

Mladen Jovanovic
Mladen Jovanovic
4,662 Points

Why does "getCurrentProgress" work in its current form?

So, I know this question could turn out to be pretty dumb, or that I may have missed the explanation earlier on in the course/lesson, but:

(Disclaimer: My code is written just like Craig's and works perfectly, soI am not questioning the results, nor am I troubleshooting why it won't run.)

This part of the code is confusing me:

public String getCurrentProgress() { String progress = ""; for (char letter : answer.toCharArray()) { char display = '_'; if (hits.indexOf(letter) != -1) { display = letter; } progress += display; } return progress; }

Shouldn't this mean that the character "letter" (e.g. "t") will be compared to the content of the "hits" string for as many times as there are characters in the "answer" string (e.g. "treehouse" - i.e. 9 times)?

Also, in this case, since "t" is a hit, why doesn't the code then add "t" to the "progress" string 9 times, resulting in the string essentially turning into "ttttttttt"?

Am I missing something here? Any feedback is appreciated. :)

luther wardle
seal-mask
.a{fill-rule:evenodd;}techdegree
luther wardle
Full Stack JavaScript Techdegree Student 18,029 Points

when a char array is made, it breaks up a string into what is essentially its parts like so: Pancakes.toCharArray() would be P-a-n-c-a-k-e-s. The for each loop will just move from "P" then to "a" then to "n" and so on until it reaches the end. At each char, the if statement evaluates to see if the letter present is in fact a letter at all. Which in your case is represented by making sure it is !=-1 this is the value of a blank space.

so the same letter is not displayed over and over, rather if the "t" index in the array is a letter and not a blank spot the progress string will add "t" to its list of stored values and move on to evaluate the next char in the same fashion until it runs out of chars to evaluate then it will return the string called progress

Mladen Jovanovic
Mladen Jovanovic
4,662 Points

Thanks! This makes sense. I'm still not entirely clear on how a lot of stuff works in Java (and sometimes googling doesn't really help.).

P.S. After I posted the question, I realized that it was in no way tied to the lesson I was following, but I don't know how to do that. :( Nor do I know how to paste the code in a way that it's different than the rest of the text (i.e. colorized and with a black background).

Mladen, to format your code, just add 3 backticks ``` on the line before your code, and 3 on the line right after. Then let the magic happens :)

2 Answers

Mladen Jovanovic
Mladen Jovanovic
4,662 Points

Hey, guys (and gals)! Thanks a lot for your replies and help. I really appreciate it. :)

System.out.println("Thank you!^_^");

You're welcome! :)

Hello, u have a HELP button in the right corner of the screen in each lesson.

if u want us to review the code in a good view u just need to type 3 backtips(java) then and ur code and then another

(```java)

your code is here

(```)

ignore the curly bracket