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 trialDaniel Makafui Ahiatrogah
3,044 Pointsprompt user in a do while loop. the loop should continue while the response is No.
You are to declare a string variable. Prompt the user continually in a do while loop with the question "Do you understand do while loops? and store the result in string variable" The loop should continue to run while the response is No
// 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?");
if(response.equals(1))
{
System.exit(0);
}
}while(response.equals(0));
}
2 Answers
Steve Hunter
57,712 PointsHi Daniel,
I've copied your code and tweaked it a little:
String response;
do {
response = console.readLine("Do you understand do while loops?");
}while(response.equals("No"));
Make sense? The while
does the testing, not an if
.
Steve.
Daniel Makafui Ahiatrogah
3,044 PointsThanks I really appreciate it
Rachelle Wood
15,362 PointsYou can simplify your code (if you are on the second part of the challenge?).
Your variable declaration is correct, your prompt is correct, but your do while loop is the problem. Just have the do while loop keep prompting the prompt that you have there and set the condition in the while part to equal no.
String response;
do {
response = console.readLine("Do you understand do while loops?");
} while (response == "No");
Steve Hunter
57,712 PointsWhile (no pun intended) testing for String equality with a double-equals can work, it is frowned upon as it can have some odd behaviours and produce inconsistent results. It's safest to use the String
class' equals()
or equalsIgnoreCase()
methods for this. :-)
Rachelle Wood
15,362 PointsThat is the first I am hearing that...
Rachelle Wood
15,362 PointsI checked up on this, and no it is not really frowned upon if you use it properly.
You are right that I did not use == properly in this case because what I did was to check for the equality between two objects. If you want to compare two string values then yes, .equals() is correct.
I guess I am getting Java confused with JavaScript where you can use === to check if the value of two strings is equal. In other languages I have played around with you can also use == or the equivalent to check for the value of two strings.
Steve Hunter
57,712 PointsIt's frowned upon in the sense that it is unlikely to deliver the result you're expecting except in some rare circs with pooled strings or interned ones. The code looks absolutely fine but can chuck out some weird errors that are hard to pin down as the use of == can be so easily interpreted simply. I only mentioned it out of good practice - it is a point worth knowing, I think.
The SO community do a better job of explaining it than I do ... there's a useful link here
Daniel Makafui Ahiatrogah
3,044 PointsDaniel Makafui Ahiatrogah
3,044 PointsThanks I really appreciate it