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

Stuck on part 2 of the Knock Knock challenge. The code seems fine to me, but i keep getting an error message.

My code is as follows:

// Person A asks: boolean answer; 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? "); answer= (who.equalsIgnoreCase("Banana")); if(who.equalsIgnoreCase("Orange")) {console.printf("Orange you glad I didn't say Banana again?");}

}while(answer);

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

Question. am I missing anything. Should I include the text in the brackets? If so how cos its messing up the code when i try to include it?

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?
    ...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.
*/

// ====BEGIN PROMPTING CODE====

// Person A asks:
boolean answer;
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?  ");
  answer= (who.equalsIgnoreCase("Banana"));
  if(who.equalsIgnoreCase("Orange")) 
  {console.printf("Orange you glad I didn't say Banana again?");}


}while(answer);



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

// ==== END PROMPTING CODE ====

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Pretty close !!!

I think if you remove the if statement it will pretty much work. After the do while loop you write the punchline. The punchline only happens after the person stops saying banana, so that is right after the loop. Make sure to use your who variable.

Here is what got me through

String who;

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

I am not sure if this is what you meant, but I've used who variable. It works on the workspace but not on the challenge. Where did i go wrong?

boolean answer; 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? "); answer = (who.equalsIgnoreCase("Banana")); }while(answer);

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

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

By the way Daniel, I am now on the 2nd part of the challenge.

m8, this is the code that got me through the whole challenge when I did it last night. It is supposed to be: String who; do { console.printf("Knock Knock.\n"); who = console.readLine("Who's there? "); console.printf("%s who?\n", who); } while (who.equalsIgnoreCase("banana"));

Your welcome.

I am not sure if this is what you meant, but I've used who variable. It works on the workspace but not on the challenge. Where did i go wrong?

boolean answer; 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? "); answer = (who.equalsIgnoreCase("Banana")); }while(answer);

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

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

Thanks a lot guys with all your help. Its fine now.