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

Stéphanie Abdel Malek
Stéphanie Abdel Malek
453 Points

Why is my code not working?

I don't get what's wrong with my code? should I add a printf statement even though its not mentioned in the question?

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean not;
do {
  response = console.readLine("Do you understand do while loops?");
  not= (response.equals("no"));
  }

2 Answers

michaelcodes
michaelcodes
5,604 Points

Hi there! So for this challenge we do not need to use a boolean. I took a look at the code here and change some stuff up. I commented out the boolean and added the "while" part of the do-while. Here is the fixed up code take a look:

String response;
//boolean not;
do {
  response = console.readLine("Do you understand do while loops?");
//  not= (response.equals("no"));//Commented out the boolean line because we dont need it
  } while(response.equals("No"));//This while checks for the response

Hope this helps, if you have anymore questions let me know! Take care and happy coding

Hi Stephanie,

Michael gave a great answer! Excluding the boolean makes the code much simpler. However, that is actually not your issue.

The problem you are having is simply because you didn't capitalize "no"! Change it to "No" (and add your while argument after the loop like Michael suggested) and you should be good to go.