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

Step 2 of 3 of Looping Until The Value Passes - how to get through?

I've currently been stuck on step 2 of Looping Until The Value Passes - the "Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop."

Despite my best attempts, I've been stuck on it for the past 4 days and have had no luck in solving my issue. Can you guys give me a hand?

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?  ");
 do {
      if (response.equalsIgnoreCase("No"))

 }

1 Answer

andren
andren
28,558 Points

The main issues with your current solution is:

  1. The line setting response equal to console.readLine should be inside of the do loop, since you want that question to be asked each time the loop runs. If it is placed outside the loop then it will only ask that question once.

  2. You do not set the running condition of a do loop using an if statement. The run condition of a do loop is set by typing while (condition) right below to the do loop.

Like this:

String response; // Declare response variable
do {
    response = console.readLine("Do you understand do while loops?  "); // Prompt the user for a response
} while (response.equalsIgnoreCase("No")); // specify the run condition of the do loop

I added some comments to specify what each part of the code does.

Perfect, I've got it! Thanks for the help