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 trialMark Richardson
397 PointsTrouble with Beginning Java Challenge Task
I'm trying to complete the challenge task and my answer appears to be in a loop and "runs too long". If you could help this newbie, I'd appreciate it.
// 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? ");
boolean isInvalidWord;
do{
isInvalidWord = response.equalsIgnoreCase("no");
}while(isInvalidWord);
3 Answers
Steve Hunter
57,712 PointsHi Mark,
You're pretty much there:
// 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.equals("No"));
So, the loop continues if the user enters "No". Otherwise, the loop will end. You could use equalsIgnoreCase
but the question doesn't require it as the instructions are clear about the format of the word No
. The loop will run at least once, ensuring that the user is prompted for their input. If the response is No
the loop runs again, prompting the user and accepting their input.
Make sense?
Steve.
Mark Richardson
397 PointsPerhaps I'm confused in that Im working in the challenge task space rather than the regular workspace. The only feedback the challenge gives is that the loop rain too long. Nothing is printed to the scree. No input is requested. Maybe I am missing something...
Steve Hunter
57,712 PointsHi Mark,
Apologies; my answer didn't help you as I wasn't clear enough.
The challenge doesn't show you the code running, unfortunately. That all happens behind the scenes as the compiler runs tests to provide input and measure the output to see if it is as expected. My description of the input/output process was esoteric. You can't see it.
So, the tests will simulate the input "No" to see if the loop repeats. They will also test that the prompt to the user is as expected. Lastly, it'll expect an answer other than "No" to terminate the loop. Your loop never terminates unless the user enters not-No at the outset. The user is never asked to re-input their response so, if you enter "No" (or "No"; "NO") the loop is recursive; it never ends. If you start with "Yes" (or anything else other than No, for that matter) the loop will run once and exit.
The tests may begin with entering the negative response, expecting to be prompted for a revised answer. That prompt never comes so your loop runs forever.
Is that a little clearer? Apologies again.
Steve.