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 trialSherman Drake
Courses Plus Student 286 PointsJava Basics Do While error
I received the following error: Make sure you are looping while the value stored in response is equal to No
// 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?");
String answer;
boolean isWrongAnswer;
do {
answer = console.readLine("Enter your answer .");
isWrongAnswer = answer.equalsIgnoreCase("no");
if (isWrongAnswer) {
console.printf("Please re read the do while loop instructions.");
}
} while (isWrongAnswer);
console.printf("%s, good answer, It's good to understand do while loops!");
2 Answers
Marek Zakrzewski
Courses Plus Student 1,288 PointsHello there ;)
I'm also a novice, so feel free to correct me, if I'm wrong.
You don't have to ask for the input twice - you did it once outside the loop, and second time inside it. Erase the 'answer' string, and leave the 'response' declared, but initialize it first inside the do while loop. It may solve your problem, and keep your code a little cleaner. I have also used the .equalsIgnoreCase(), to compare it to the response string inside mine while parenthesis. You might want to try it out too, but I don't think there should be a problem with your way.
I hope I was able to help. :) Best regards, Marek
Christian Andersson
8,712 PointsYour loop seems like it should be working alright, though there is one error with your last printf
; you forgot to add the parameters for the %s
.
console.printf("%s, good answer, It's good to understand do while loops!", answer);
The code-challange says there is an error with your loop though, so perhaps this won't fix your issue. Try it, and let me know!
EDIT:
Alright, I tried the code-challange my self and here is what I did:
// 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 == "No");
console.printf("Because you said %s, you passed the test!", response);
Still not sure what's wrong with your code, because the loop really should work the way you've written it. But perhaps the code-challange doesn't like something else in your code and gives you that message even though the loop it self is correct. Though you have some prints and other stuff that seems improvised. The code-challange system is usually picky on improvised code, even if it's not "wrong" -- try and follow the instructions exactly as they are presented.
Sherman Drake
Courses Plus Student 286 PointsHi Christian, thanks for your reply! Good catch on the %s parameters. I updated my code and received the same error. I received another thing to try so I will check that out.
Christian Andersson
8,712 PointsSherman Drake check updated answer above.