Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Looping until the value passes

where am i wrong?

(isValidAnswer) = False

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 isValidAnwer;
do {
 who = console.readLine("Who's there?  ");
  isValidAnwer= (who.equalsIgnoreCase("Banana"));

  if (isValidAnswer)=false {
// Person B responds:
    console.printf("Please try another answer."); }
} while(isValidAnswer)=false
console.printf("%s you glad  i didn'nt say Banana again?");

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

1 Answer

Christopher Augg
Christopher Augg
21,223 Points

Admire,

You are very close. Good job. There are a few errors we can go over and get you back on track.

The do while block should have started right after this....

      // ====BEGIN PROMPTING CODE====                    

because we want the console to always display "knock Knock" until the do while loop fails.

           String who;

While a Boolean can definitely be used, it is not needed for this exercise as it is more efficient to only test the equality for the do while block. Furthermore, you misspelled isValidAnswer as isValidAnwer twice.

          boolean isValidAnwer;

          do {
                who = console.readLine("Who's there?  ");
                isValidAnwer= (who.equalsIgnoreCase("Banana"));

When testing with an if statement, you must test within the parentheses and make sure to understand the differences between using = and == because the first is an assignment operation and the second tests for equality. The same goes for the test for the do while and then a ; is required after a do while block as well.

              if (isValidAnswer)=false {   
               // Person B responds:

This line was not asked for in the instructions nor do you need the if statement.

          console.printf("Please try another answer."); }
          } while(isValidAnswer)=false

You forgot to place who like so: console.printf("%s you glad I didn't say Banana again?", who);

     console.printf("%s you glad  i didn'nt say Banana again?");

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

Please follow my comments along with the following code and be sure to ask any further questions if you do not understand.

   String who;
   // ====BEGIN PROMPTING CODE====
   do {
         // Person A asks:
         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);
   }while (who.equalsIgnoreCase("Banana"));

// ==== END PROMPTING CODE ====
console.printf("%s you glad I didn't say Banana again? ", who);

thanx a lot