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 trialmanlau
364 PointsAny problems with my do while loop?
I am working on a task of a do while loop. The question goes like this: prompt the user in a do while loop. The loop should continue running as long as the response is No.
My answer: String response =console.readLine("Do you understand do while loops?"); boolean answerIsNo; do{ answerIsNo = (response.equalIgnoreCase("no")); if(answerIsNo) { console.printf("are you sure? ");
}
while(answerIsNo);
}
Any problem with my code??Thanks
// I have initialized a java.io.Console for you. It is in a variable named console.
String response =console.readLine("Do you understand do while loops?");
2 Answers
Paul Ryan
4,584 PointsString response;
do{
response = console.readLine("Do you understand do while loops?");
}while(response.equals("No"));
You can simply just check the response value each time. This eliminates the need for you to use a boolean as the equals method will return a boolean.
if you have any queries about this, just ask.
Daniel Hartin
18,106 PointsHi Manlau
It looks to me like your loop syntax is correct however if you are trying to pass the task you have linked to you, i'm not quite sure.I have passed the task you have linked to using the code below if you need something to compare to.
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = "";
do{
response = console.readLine("Do you understand do while loops?");
}while(response.equals("No"));
console.printf("Because you said %s, you passed the test!",response);
P.s if you get chance follow the link on this page (marked: markdown cheatsheet) for information on syntax highligthing as it makes it really hard to read your code without it :)
Thanks Daniel