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

what is it asking me to do with the joke

To me this challenge doesn't make sense can someone help me

/*  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?
    ...as long as Person A has answered Banana the above repeats endlessly
    ...assuming the person answers Orange we'd see
    Person B:  Orange who?
    ...and then the punchline.
    Person A:  Orange you glad I didn't say Banana again?
    (It's a really bad joke that makes it sound like "Aren't you glad I didn't say Banana again?")

    Let's just assume the only two words passed in from the console from Person B are either banana or orange.


// Person A asks:
 console.printf("Knock Knock.\n");

// Person B asks and Person A's response is stored in the String who:
String who = console.readLine("Who's there?  ");

// Person B responds:
    console.printf("%s who?\n", who);

Timothy Williams
Timothy Williams
1,536 Points

You want to make a do-while block around the input line checking to see if the user answers something other than Banana before proceeding to finish the code


In this case

   String who = console.readLine("Who's there?  ");

console.printf("Orange you glad I didn't say Banana again?\n");

It is wanting you to create a do/while loop, so basically repeat the "knock knock","who's there", "banana","banana who?"
so put the entire code in a DO block and create a WHILE block, while (who.equalsIgnoreCase("banana")); But make sure you declare the who variable outside of the DO block, so put string=who before the DO

1 Answer

Rob Bridges
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there,

What this challenge is wanting you to wrap the entire joke in a do while loop.

The syntax is like below.

do {

// run through this code until the while condition is not met. 

} while (//condition that breaks loop);

So basically the first task is wanting you to put the whole condition in a loop and have it continue to go through the joke until who is not banana

We can do this by the follow

String who;
do {
console.printf("Knock Knock.\n");

// Person B asks and Person A's response is stored in the String who:
 who = console.readLine("Who's there?  ");
} while (who.equalsIgnoreCase("banana"));
// Person B responds:
console.printf("%s who?\n", who);

Notice we declare who outside of the loop, this is because during the second task of this assignment, we will need to use who again, and if it was only defined in the loop, we wouldn't have the scope to access it.

Thanks, let me know if this doesn't help.