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

Naya Willis
Naya Willis
2,461 Points

Help

Can somebody solve this one so I can review it to see what I was actually supposed to do. I felt like i was very close but not on point.

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:
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);

// ==== END PROMPTING CODE ====
Naya Willis
Naya Willis
2,461 Points

It said to add the do-while loop. Then wrap it in the code block

3 Answers

Axel McCode
Axel McCode
13,869 Points

Here is code to solve the challenge. I added comments help you better understand it. Hope this helps :)

*  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:
console.printf("Knock Knock.\n");

// Person B asks and Person A's response is stored in the String who:
String who = "";  // created string outside the do-while loop so it is in scope of the loop
do{who = console.readLine("Who's there?  ");  // stores the string entered in the String variable "who"
  }while(!who.equalsIgnoreCase("orange"));    // checks if who is equal to "orange"
/*
*notice the not operator (!), this will make the conditional statement return the opposite of what it
*turns out to be. So if the String variable "who" is equal to "Orange", it would return true, however *because the "not" operator is there, it will return the opposite, which is false, in this case do-while 
*loop will stop looping.
*/

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

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

Hi Naya, see if this Google search is able to help resolve your issue.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Or you can use who.equalsIgnoreCase("banana") for the condition. We do not go over negation in Java Basics.