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

Reed Williams
Reed Williams
3,323 Points

print out punchline

I've tried to complete this a few different ways. This code is just my last attempt. I realize it's throwing task 1 off. I believe I am unclear on what the question wants.

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.equals("banana"));

console.printf("Orange\n");
who = console.readLine("------ who?\n");
console.printf("%s you glad I didn't say banana again?", who);

2 Answers

Charlie Thomas
Charlie Thomas
40,856 Points

This is the correct code:

//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.equals("banana"));
console.printf("%s who?", who);
console.printf("%s you glad I didn't say Banana again?", who);

You got the loop write but for the punchline you have to print "Orange who?" and "Orange you glad I didn't say banana again?" using the who variable. But I agree the code challenge is badly phrased.

Reed Williams
Reed Williams
3,323 Points

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