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

I am having trouble with Java Basics

Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop.

Example.java
// 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.equalsIgnoreCase("no"));
if{
  response = "yes" console.exit;
}

2 Answers

Michael Hess
Michael Hess
24,512 Points

Hi Joey,

The do-while loop is a post-test loop. A post-test loop runs the code inside the loop body at least one time then evaluates the looping condition after the code in the loop body is ran. So, there is no need for an if-statement in this exercise.

//Declare a String variable named response, then initialize it using double quotes
String response;


// The do while loop keeps asking the same question until "yes" is entered
//So we want to keep repeating when "no" is typed
// Exit the loop when "yes" is entered

//task 2

do{
response = console.readLine("Do you understand do while loops?");  // "yes" or "no" is stored in the response variable 
// when someone types "no" the loop repeats
// when someone types "yes" the loop is exited
}while(response.equalsIgnoreCase("no"));

If you have any other questions feel free to ask! Hope this helps!

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hola,

You are close, String = response is the same as saying String the object is equal to response. That can't happen. to define a variable without giving it a value. It's as follows:

String response;

The best part of this is that because it's defined at the class level. Scope isn't a worry and you can change the value through out the class.

Next, you are assuming that if I don't say No, that I am going to say yes. What if I said maybe? Then the program would fail. I didn't choose yes or no.

do{
  response = console.readLine("Do you understand do while loops?");
 }while(response.equalsIgnoreCase("no"));
if{
  response = "yes" console.exit;
}

See, you have a while loop that runs 1 time no matter what. But if I choose maybe, it breaks. If I say anything other than No. It leaves the do while checks if it's yes and exits. But if it's not yes what then?

String response;

do{
  response = console.readLine("Do you understand do while loops?");
  if (response.equals("No")) {
    console.printf("Because you said %s, you passed the test!", response); 
  }
 }while(response.equals("No"));

Now see above. I define the variable above the do while. Now, I ask a question. IF the question comes back as No, then I print awesome you passed the test. Then it goes down to the while, and says oh sweet. It's No so it quites. Now if I typed yes, maybe, food, dog. Wouldn't matter. It would just keep going until it got No. There are several ways you could do it, you could of defined a boolean and set it in the if statement or not. But hey, less is more and do what you know.

Let me know if that helps.

Thanks!