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 need help on this exercise. Can anyone help out a beginner?

It has 2 errors according to the compiler. The problem is PersonA and personB.

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====
String PersonA = console.printf("Knock Knock.\n");
String who;
boolean isInvalidWord;
do {
  who = console.readLine("Who's there?      ");
  isInvalidWord = (who.equalsIgnoreCase("banana")||
                  who.equalsIgnoreCase("BANANA"));
  if (isInvalidWord) {
    console.printf("Try again.\n\n");
  }
} while (isInvalidWord);
String PersonB = console.printf("%S who?\n", who);

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

Jason, do you mind setting this post to answered and post your finished code so it can be able to be a resource to others, I remember this was a hard one for a lot of people, and I could find the code for hours. Thanks

uhmm...... How do I set it to the answered post? I am very new to this and do not see the option to do that.

Ken Alger
Ken Alger
Treehouse Teacher

Jason;

At the bottom of each answer there is a Best Answer link which will mark the selected answer as... you guessed it, the "Best Answer." In doing so it puts an indicator in the forum that the post has been resolved, awards the person who provided the answer with additional points, and creates a happier and friendlier forum. All with one click. :smile:

Happy coding,

Ken

2 Answers

I did this one recently, and it's actually looking for something a bit simpler than what you have. There's no need to create PersonA and PersonB, you just need to add a do-while loop to the existing code so that the first four lines are repeated as long as "who" equals "banana."

Thank you for your reply. I was able to achieve the end result through watching and re-watching the video until it made sense.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jason;

Welcome to Treehouse!

I answered a forum post on this challenge here, take a look and post back if you still have questions.

Happy coding,

Ken