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 trialTom Douglas
365 PointsStep 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?
// 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
28,558 PointsThe main issues with your current solution is:
The line setting
response
equal toconsole.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.You do not set the running condition of a
do
loop using anif
statement. The run condition of ado
loop is set by typingwhile (condition)
right below to thedo
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.
Tom Douglas
365 PointsTom Douglas
365 PointsPerfect, I've got it! Thanks for the help