Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Looping until the value passes

Mark Richardson
Mark Richardson
397 Points

Trouble 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.

Example.java
// 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

Hi 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
Mark Richardson
397 Points

Perhaps 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...

Hi 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.