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

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,558 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.