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

khoreysmith
khoreysmith
3,524 Points

I keep getting a communication error when I hit preview. I've done exactly what the question asks. What is the error?

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

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
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there?  ");
do {
console.printf("%s who?\n", who);
} while (who.equals("banana"));

1 Answer

Hi,

your printf for Knock Knock is outside the loop. Therefore it only get's executed once, while it should be executed everytime the user types in banana. The same is true for the assignment to the variable who of the readLine function.

It should look like this:

String who = "";

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


// The rest for the case "orange" goes here...

Can you take it from here?