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

I having problems understanding the question here

So this is the last challenge right now and I just really don't understand the question. Do you want mew to wrap that whole chunk of code into a loop? Do you want that do loop to execute while who = "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

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

2 Answers

Hi Jarell,

You'll need to move the String who declaration outside (above) the do while loop. Also, when checking for equality use ==, because = is the assignment operator. Here is some example code to help get you started.

String who = "";
do {
    // add your code here
} while (who == "banana");

Thank you so much man. You even helped me learn something :D

Craig Dennis
Craig Dennis
Treehouse Teacher

You should actually use who.equals("banana"). String equality is tricky!

I'm struggling to fully grasp the question being asked.

I've tried countless times but can't quite understand where I'm going wrong.

My latest attempt is as follows:

boolean who;

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

console.printf(who);  
console.printf("%s who?\n", who);
console.printf("%s you glad I didn't say Banana again?")} 
}

I know I've gone a bit off the rails with this particular attempt, so just pointing me in the right direction would be great.

Craig Dennis