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 trialMáté Batik
1,650 PointsDon't know where is the problem :(
where is the error here?
String response;
Boolean isInvalidWord;
do {
response = console.readLine("Do you understand do while loops?");
isInvalidWord = (response.equalsIgnoreCase("No"));
if (isInvalidWord) {
console.printf("Try!");
} while (isInvalidWord);
Sysytem.exit (0);
}
Máté Batik
1,650 PointsThanks! :)
2 Answers
Steve Hunter
57,712 PointsHi there,
Foxtails has identified where you've gone a little astray with this one! However, this task can be simplified still further.
You start by creating a string
variable named response
, as you have done. Then you enter a do:while
loop. As soon as you enter the loop, prompt the user using the line from the first task; you have done this too.
You now have the user input. This is what the while
loop is looking for. It wants to continually loop while
the response is "No". So, the condition inside the while
statement can be response.equals("No")
. This removes the need for boolean
variables. Just test the response
; if it is "No", loop, if not - go to task 3.
String response;
do {
response = console.readLine("Do you understand while loops?: ");
} while(response.equals("No"));
// task 3 goes here - the initial post hadn't reached that yet.
I hope that makes sense.
Steve.
P.S. - points of note/detail. The question spcifically asks to test against "No"; capitalised. Yes, you could catch other user input by using equalsIgnoreCase
but that's not required of the question - it works, is probably best practice, but isn't needed here. Next, naming variables/methods 'invalid' is fine, but only if the response is invalid - "No" is a sensible, valid answer to your question - adopting clear naming conventions comes later in the course - just something to think about for now.
Máté Batik
1,650 PointsThank you! :)
foxtails
3,655 PointsYou are welcome! Doing a great job here, keep coding!
foxtails
3,655 Pointsfoxtails
3,655 PointsHi, checked the exercise. You don't need System.exit(0) there at all as that will exit from the program. Also the answer you are printing is different from challenge. It's "Because you said <response> you passed the test!". Which means you have to use string variable %s, when printing the text. So unless we are looking at two totally different challenges, it should look something like this:
Sorry, I have no idea yet, how to make screenshot, so it's clearer, but hope it will help!
[MOD: edited code block]