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

Diyan Aleksiev
Diyan Aleksiev
4,142 Points

do while loop

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

Example.java
String response;
String AnswerNo;
do {response = console.readLine("Do you understand do while loops? ");
   }while (AnswerNo.equals("No"));

1 Answer

Hi there,

You're really close - this code is throwing a syntax error due to AnswerNo not being initialized (it isn't given a value when declared or in the loop and then it tries to compare it to something at the end, which is throwing an error).

It's worth noting that you don't actually need the AnswerNo variable, though - response is the one you're changing, so that should be the one you compare to at the end of the loop. The code should look something like:

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

If you use AnswerNo as the comparison at the end of the loop, it will never pass, because AnswerNo is never changed - it will loop forever. Comparing to response allows the user to answer something other than "No", which will allow the loop to stop.