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 Basics Perfecting the Prototype Looping until the value passes

I'm in Task 2 of 2 in this Challenge and I just don't know how to proceed. Feels like I'm missing something obvoius.

So far I've got this

String who; boolean validWord; do { console.printf("Knock Knock.\n"); who = console.readLine("Who's there? "); validWord = (who.equalsIgnoreCase("banana")); if (validWord) { console.printf("%s who?\n", who); } } while (validWord);

String who1; boolean validWord1; do { who1 = console.readLine("%s who?: ", who); validWord1 = (who1.equalsIgnoreCase("orange")); if (validWord1) { console.printf("%s who?", who1); console.printf("%s you glad I didn't say %s again?", who1, who); }} while (validWord1);

Am I just going about this the wrong way for part 2?

5 Answers

Stone Preston
Stone Preston
42,016 Points

you dont need to add a second loop. just print out the punchline after the do while loop. you need to use a format specifier and the who variable using printf:

String who;
boolean validWord;
do {
  console.printf("Knock Knock.\n");
  who = console.readLine("Who's there?  ");
  validWord = (who.equalsIgnoreCase("banana"));
  if (validWord) {
  console.printf("%s who?\n", who);
  }
} while (validWord);

//print the punchline out after the loop. the value of who will be inserted in place of the %s
console.printf("%s you glad I didn't say Banana again?", who);

Thanks for the prompt reply! I think I overthought way too much there

Adam Sims
Adam Sims
6,144 Points

That was driving me crazy too. Like you, I had over thought it.... COMPLETELY. Thanks for the help!

Ho Sun Lee
Ho Sun Lee
2,003 Points

I see what you guys did there, but why aren't we emphasizing orange at all?

The punchline is "orange you glad I didn't say banana", but with this code, it could easily be "apple you glad I didn't say banana?", or whatever fruit you input.

Or is it up to the user to input the correct "answer"?

I'm sorry, this challenge confused the crap out of me...

Yes, this challenge was insanely confusing. I think the 2nd part might benefit from a re-wording. Also wasn't too obvious that we were needing to use validWord and led me to basically over think the entire challenge.

Ya I agree, if the compiler is going to be critical over what we declare like the use of "validWord" it needs to be handed to us. Don't let us sit here until we want to throw our screen threw the wall and then come here to realize its because I was using "punchline" instead of "validWord".