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 trialKevin Jervis
2,600 PointsDo While Loops
Hi guys. I'm a little stuck.Think I'm about 80% there :o(
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do you understand loops?");
do {
response = console.readLine("Do you understand loops?");
if (response.equals("No");
console.readLine("Do you understand loops?");
}
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi Kevin,
you're making a good start but there's a few issues I can see. First, you don't need to use an if statement or prompt the user a third time.
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do you understand loops?");
do {
response = console.readLine("Do you understand loops?");
} while (response == "No");
You want to keep prompting as long as the answer from the user is "No". You can do the condition you'd otherwise write in an if statement after the while keyword.
I'll leave you with the last part of the challenge! :)
Steve Hunter
57,712 PointsHi Kevin,
You're pretty much there. Try something like this:
String response;
do{
response = console.readLine("Do you understand loops?");
}while(response.equals("No")); // like what's in your if brackets
Make sense?
Steve.
Kevin Jervis
2,600 PointsThanks for breaking that down for me Steve. Makes sense now. I appreciate the help :o)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Jonathan,
Your code will prompt the user twice at the initial loop and comparing strings with
==
isn't best practice;.equals()
is the preferred way, as the OP had done.Steve.
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 Pointsequals
didn't pass the code challenge when I tested it. That's what I expected, so the only other method is==
` which passed the challenge. Odd.Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsMust've just been having a 'moment' - working fine now. Hey ho - not a problem.
Kevin Jervis
2,600 PointsKevin Jervis
2,600 PointsThat's awesome. I though I over complicated things. That's brilliant. Thanks Jonathan :o)