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

Brandon Wash
PLUS
Brandon Wash
Courses Plus Student 1,186 Points

How to print out the punchline

How to printout the punchline?

KnockKnock.java
/*  So the age old knock knock joke goes like this:
        Person A:  Knock Knock.
        Person B:  Who's there?
        Person A:  Banana
        Person B:  Banana who?
        ...This repeats until Person A answers Orange
        Person A:  Orange
        Person B:  Orange who?
        Person A:  Orange you glad I didn't say Banana again?
*/

//Here is the prompting code
String who;
do {
console.printf("Knock Knock.\n");
  who = console.readLine("Who's there?  ");
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));

3 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

Put the last console.printf() after the do-while loop ^^

You have to move a couple of things from inside the loop to before the loop too, just so you know ~.^

Brandon Wash
PLUS
Brandon Wash
Courses Plus Student 1,186 Points

Thank you . This is very confusing but practice is key importance

If you find that someone has a best answer to your question, it is always appreciated for that person to be chosen. Thanks Brandon Wash!

Well, you just want to utilize the value of who in the punchline after the code has broken out of the do-while statement. That means all you have to do is continue utilizing the %s placeholder and the who variable along with the punchline script. Add the following 3 lines to the end of the do-while block:

console.printf("%s\n", who);
console.printf("%s who?\n", who);
console.printf("%s you glad I didn't say Banana again?", who);

Which will now look like this:

/*  So the age old knock knock joke goes like this:
        Person A:  Knock Knock.
        Person B:  Who's there?
        Person A:  Banana
        Person B:  Banana who?
        ...This repeats until Person A answers Orange
        Person A:  Orange
        Person B:  Orange who?
        Person A:  Orange you glad I didn't say Banana again?
*/

//Here is the prompting code
String who;
do {
console.printf("Knock Knock.\n");
  who = console.readLine("Who's there?  ");
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));
//Person A has no longer entered banana as their answer 
//Now it executes this code (the final punchline)
console.printf("%s\n", who);
console.printf("%s who?\n", who);
console.printf("%s you glad I didn't say Banana again?", who);