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

Ulla Scheler
Ulla Scheler
1,177 Points

While looping, received the following output: "Your code took too long to answer". I can't find my mistake.

The code is below. Thank you!

5 Answers

Cristian Altin
Cristian Altin
12,165 Points

If you don't have any way to change the who variable INSIDE the do...while it will output the same string indefinitely when answering banana hence the infinite loop.

You should readLine inside the do.

Hey Ulla!

Please could you post the code you are having problems with so I can help you out?

Thank you and have a great day!

-Luke

Ulla Scheler
Ulla Scheler
1,177 Points

Hey Luke!

Thank you for responding so fast. I thought it would tag the code automatically, sorry.

Nevertheless, here it is!

-Ulla

// ====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:

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

do {

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

} while(who.equalsIgnoreCase("banana"));

Ulla Scheler
Ulla Scheler
1,177 Points

Thank you Cristian, that makes perfect sense! Have a nice day!

Cristian Altin
Cristian Altin
12,165 Points

Your welcome, glad to be of help. Enjoy your learning!! Have a nice day too!!

Hopefully this is still being followed... Same problem with taking too long to answer. I keep getting a connection error though as well. Is this typically due to an infinite loop?

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

do { if (who.equalsIgnoreCase("orange")); { console.printf("orange you glad I didn't keep saying banana?"); }

}while (who.equalsIgnoreCase("banana"));

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

Cristian Altin
Cristian Altin
12,165 Points

Same issue Joseph. You need to be able to readLine the who variable INSIDE the "do ... while" loop otherwise you will only need one "banana" to make the who variable "eternally" stuck within the loop since you are giving no chance to change it once the do is run once. So nothing changes after the first loop and the loop will run indefinitely.

Be sure to understand how a do...while works: it will execute the do part first then check if the condition is met to be able to leave the loop. Else it will repeat doing so until the condition is NOT met anymore. Even if the who variable is something different from banana the do part will be run once because the condition is checked at the end of each "run". Do...while loops are tricky because you are the one who needs to provide a way to change whatever is checked and safeguard from infinite loops.

In this case if you don't type banana it will run the "if" inside the "do" and exit but if you do type "banana" you will have to give a chance to change it into something different to exit at some point. That's why you need to readLine INSIDE the loop.