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 trial
Von Schwandt
1,586 PointsDo While Loops + If, Then Question
This question involves the final challenge in Java Basic, "Looping until the value passes". I was able to complete the challenge just fine, but wanted to explore the code further. The code challenge has you put "No" as an invalid value. When you compile the program, as long as you type anything else in response to the question "Do you understand do while loops?" the system message reads "Because you said <response>, you passed the test!". So you can type it nope, nah, peanut butter and jelly sandwich...and still get the same success system message. I tried to problem solve this next part, but was unsuccessful.
Is there a way to make it so instead of an invalid value, there is a valid value, and anything you put in besides that valid value gives you a try again message?
(I tried to use != but my syntax may have been off)
Example of final compiler:
"Do you understand do while loops?"
Response: Yes (equalsIgnoreCase)
System Message: "Because you said 'Yes' you passed the test!"
"Do you understand do while loops?"
Response: Nooope.
System Message: "Try again."
String response;
boolean invalidWord;
do {
response = console.readLine("Do you understand do while loops?" );
invalidWord = (response.equalsIgnoreCase("no"));
if (invalidWord) {
console.printf("Rewatch the video and then try again.");
}
} while(invalidWord);
1 Answer
Richard Lu
20,185 PointsThis is to answer your question: "Is there a way to make it so instead of an invalid value, there is a valid value, and anything you put in besides that valid value gives you a try again message?"
Your idea of using the "not equals" operator is definitely one way to go. Here's another way using the bang (!) operator.
String response;
do {
response = console.readLine("Do you understand do while loops?")
if (!response.equalsIgnoreCase("yes")) { // bang used here
console.printf("Rewatch the video and then try again.");
} else {
console.printf("Because you said 'Yes' you passed the test!");
}
} while(!response.equalsIgnoreCase("yes")); // and here
This code above does exactly what you want it to do. Let me know if there are any questions :)
Von Schwandt
1,586 PointsVon Schwandt
1,586 PointsSorry it took me so long to get back to this response, but I wanted to thank you! Your help is greatly appreciated.
Richard Lu
20,185 PointsRichard Lu
20,185 PointsNo problem Von! I'm always looking to help :)