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

Yiyi Chen
Yiyi Chen
5,167 Points

Java basic looping

In the looping challenge question, I write a code like do{ console.printf("%s\n", who); console.printf("%s who\n", who); }while(who.equalsIgnoreCase("banana);

System.exit(0);

And it say it takes too long to run my code and shut down. I do really know what this question expecting.

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 = console.readLine("Who's there?  ");

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

System.exit(0);

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

1 Answer

Stephen Bone
Stephen Bone
12,359 Points

Hi Yiyi

Apologies as I'm not able to test it but does this not just create an infinite loop where it just endlessly prints the two lines below which could explain why it's timing out.

    console.printf("%s\n", who);
    console.printf("%s who?\n", who);

EDIT: Now I've had the chance to sit down and look at the task you also need to bring the Knock Knock and console.readLine method into the do while loop so rather than it just endlessly printing it comes back around to the prompt and waits for user input. And the last part of this task is to move the String who declaration outside of the do while loop so it can be accessed by code outside of it.

So if that made any sense your code should look something like below:

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"));

Hope it helps!

Stephen