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

Thomas Minnick
Thomas Minnick
2,539 Points

Stuck on "do while" loops in java basics challenge

I have no idea what I'm doing wrong but it looks very close if not the same to my working "TreeStory"

Example.java
String question = console.readLine("Do you understand do while loops?");
String response;
Boolean no;
do {
response = (question);
no = (response.equalsIgnoreCase("No"));
  if(no) {
    console.printf("Try again");
  }
} while (no);

1 Answer

andren
andren
28,520 Points

The question variable does not contain the console.readLine command, it contains the result of calling that command. In other words the text input that was entered in the console.

That means that this line:

response = (question);

Simply sets response equal to the first response entered. It does not rerun the console.readLine command which is needed to get new input from the user.

If you simply use the console.readLine command directly without creating a question variable like this:

String response;
Boolean no;
do {
response = console.readLine("Do you understand do while loops?");
no = (response.equalsIgnoreCase("No"));
  if(no) {
    console.printf("Try again");
  }
} while (no);

Then your code will work.

Also while this won't cause an issue for this task it's worth mentioning that the challenge did not ask you to use an if statement to print out an error message. Including any code that is not explicit asked for by the task instructions is generally a bad idea, as these challenges tend to be pretty picky and strict about how the program is coded.