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

Filimon Armenis
Filimon Armenis
1,162 Points

Can't find the right code, where can I find correct answers?

And somehow I think I get an infinite loop, any help?

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.
*/





// 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  bananaResponse;      
boolean  orangeResponse;       

    who = console.readLine("Who's there?  ");    

                  do {
                       bananaResponse=(who.equalsIgnoreCase("Banana"));
                      {
                       who=console.readLine("%s who?\n", who); 
                      }
                } while (bananaResponse);



               do { 
                  orangeResponse=(who.equalsIgnoreCase("Orange"));
                {
                  console.readLine("Orange you glad I didn't say Banana again?");

                }
                  } while (orangeResponse);
Edwin Amaya
Edwin Amaya
Courses Plus Student 809 Points

``` String who;
boolean bananaResponse;
boolean orangeResponse;

who = console.readLine("Who's there?  ");    

              do {
                   bananaResponse=(who.equalsIgnoreCase("Banana"));
                  {   <----------------what is that open curly bracket for?? 
                   who=console.readLine("%s who?\n", who); 
                  }
            } while (bananaResponse);



           do { 
              orangeResponse=(who.equalsIgnoreCase("Orange"));
            {   <----------------------same here??? 
              console.readLine("Orange you glad I didn't say Banana again?");

            }
              } while (orangeResponse); ```

1 Answer

Stephen Bone
Stephen Bone
12,359 Points

Hi Filimon

First up the structure of the do while loop isn't quite right as you need to put all of your statements within a single set of curly brace.

It looks like you're trying to use the console.readline() method to print text instead of the console.printf() method. Remember we use the readLine method to get input from a user.

We also only need one do while loop as it will only loop as long as the response is banana. As soon as a different response is given we will leave the loop and print out the punchline.

So the code should now reflect what we have below:

String who;      
boolean  bananaResponse;              

do {
  who = console.readLine("Who's there?  "); 
  bananaResponse=(who.equalsIgnoreCase("Banana"));
  console.printf("%s who?\n", who); 
} while (bananaResponse);

// Person B responds:
console.printf("%s you glad I didn't say Banana again?\n", who);

One other thing although it will work either way is the whole setting and testing bananaResponse variable is a bit needless. You could simplify your code by calling equalsIgnoreCase() directly in the while condition as below:

String who;

console.printf("Knock Knock.\n");

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

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

Hope it helps!

Stephen