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

Phong Tran
Phong Tran
640 Points

It there a work around when you get the error message your code is taking to long to run

Here is my code I am trying to run. When I do so I get the error message code is taking to long to run. Wondering if there is a work around to I know if I got the correct answer. Plus I want to continue with the lesson but can not get past with error message.

String who;
do{
console.printf("Knock Knock.\n");
who = console.readLine("Who's there?  ");
console.printf("%s who?\n", who);
}
while(who == "banana");
do{
console.printf("Orange\n");
who = console.readLine("Orange who?  ");
console.printf("%s who?\n", who);
}
while(who == "orange");  
console.printf("Orange you glad I didn't say Banana again?");
Joseph Turnquist
Joseph Turnquist
14,516 Points

Just so you know, Team Treehouse provides markup syntax for posting code in the forums.

String who;

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

do{
  console.printf("Orange\n");
  who = console.readLine("Orange who? ");
  console.printf("%s who?\n", who);
} while(who == "orange");

console.printf("Orange you glad I didn't say Banana again?");

3 Answers

Hi Phong,

You only need 1 do/while loop around the 3 lines of starter code that you were given. Once "Orange" is given as input then that will end the loop and you need to print the punchline right after that, not loop again with orange.

So your first loop is good except on the condition you need to use .equalsIgnoreCase

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

Then right after that you'll print the punchline "Orange you glad I didn't say Banana again?" except you want to use the who variable to do that. The who variable at this point is storing "Orange".

Phong Tran
Phong Tran
640 Points

Thanks for all the help guys got it, you guys are so fast with the responses, impressive

Phong Tran
Phong Tran
640 Points

Just wondering how you guys posted the highlighted syntax boxes.

Sorry, I meant to post it.

This thread will show you the different ways you can post code in the forums: https://teamtreehouse.com/forum/posting-code-to-the-forum

Joseph Turnquist
Joseph Turnquist
14,516 Points

I also forgot! Glad Jason was able to respond with that article.