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

martin li
martin li
1,026 Points

Why is this wrong?? HELP ASAP!!

Now let's have our code make a joke. Read the code and comments below.

We want to move that prompting code into a do while loop. Wrap the code into a do while code block and check in the while condition to see if who is "banana" so that the loop continues.

HINT: Remember to move your who declaration outside of the do block so that it can be accessed.

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;
boolean response;
boolean orangse;
do{
  who = console.readLine("Who's there?  ");
  response = who.equalsIgnoreCase("banana");
  orangse = who.equalsIgnoreCase("orange");
  console.printf(who);
  while (response){
console.printf("%s who?\n", response);
  }
  if (orangse) {
    console.print("Orange you glad I didn't say Banana again?");
  }
}


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

3 Answers

You are complicating things. for the first stage you can try the code below.

String who;
// Person A asks:
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?  ");
     console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));

After passing the first stage then for the second stage try the code below. if it helps please click best answer and if it doesnt ....give me a shout

if (who.equalsIgnoreCase("orange")){
console.printf(" %s you glad i did not ask for banana",who);
}
Brad Penney
Brad Penney
8,424 Points

This works perfectly. However, I encourage others to simply read this code and try to find where you went wrong with your own - don't copy and paste. You'll learn more...

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

You are trying to make it a lot harder than it is . Here is a lot easier version ;)

while (who.equalsIgnoreCase("Banana")){
  who = console.readLine("%s who?\n", who);
}
console.printf("%s you glad I didn't say Banana again?", who);
Raymond Osier
Raymond Osier
16,581 Points

I got it to work this way ```java String who; // Person A asks: 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? ");

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

// ==== END PROMPTING CODE ==== while(who.equalsIgnoreCase("banana")); if (who.equalsIgnoreCase("orange")){ console.printf(" %s you glad i did not ask for banana",who); }

remember to define the String Variable outside the code block so it can be used as global variable and not just  a local variable or so that is can be seen outside the code block