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

Henry Aaron
Henry Aaron
1,213 Points

i can't get passed a challenge my code took too long to run

Im pretty sure I'm stuck in an infinite loop with my code and i don't know how to fix it

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
  response = console.readLine("Do you understand do while loops?");
if (response.equals("No")) 
{
}
while (response.equalsIgnoreCase("No")) 

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

Hi Henry,

The challenge is looking for a do-while loop, so let's use that syntax. Essentially, we are telling the program to do something again and again until something specific occurs. In the case of the do-while loop syntax, we'll set our condition for the loop to end with the while clause only.

We'll move the prompt inside of the loop, because that is what we want to run again and again until we get our desired answer. We'll keep the actual variable declaration outside of the loop, so that the declaration doesn't attempt to run again.

We are saying: Let's run, or do, the prompt again and again while the user hasn't given us a no answer (yet.)

We'll make sure that the while statement ends with a semi-colon, since the compiler needs to know that we want to terminate the statement there.

String response;

do
{
    response = console.readLine("Do you understand do while loops?");
}
while (response.equalsIgnoreCase("No"));

Hope this helps!