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

Java Basics-Stage 3: Extra Credit

My instructions are:

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====

And it gives me:

// 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 ====

  1. When "banana" is answered I don't know how to repeat it endlessly.
  2. I don't know how to do the "do" and "while" methods.
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 ====

The syntax for a do while loop is .

do{}while( <condition> );

So you want to put all of the code inside this loop. The condition should check if the variable who is equal to " banana ". Remember to use .equals instead of ==.

Also you need to declare the string who outside of the loop, so that it has the correct scope to be used later.

It may pass but it is a bad practice.

== tests for reference equality, (are they the same object?) .equals tests for value equality.

In this case you want .equals. Don't teach bad practices.

Ah, my mistake. You are correct. I will just delete my comment then.

3 Answers

I don't really want to just give you the answer.

Use the following template to get you started.

// Declare and initialize the String variable who, do this outside the loop so that it can be used later outside of the loop

do{

      //print the "Knock Knock.\n" to the console
      //Read the line into the string who , using the prompt "Who's there?  "
      //print the "%s who?\n"? response the the console

}while(  use .equals to check if the variable who contains the value "banana"   );

//The variable who can be used here, outside of the loop because it was declared before(outside) the loop

If you have any specific questions I am happy to help you out, I don't see the point in giving you the answer. Sorry.

I didn't really get that could you give me the answer?

Thank you so much!