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

Luuk de Reus
Luuk de Reus
1,936 Points

How to make a loop in java?

Prompt the user with the question "Do you understand do while loops?" Store the result in a new String variable named response.

I don't understand!

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

1 Answer

Hi Luuk,

The first part of the challenge asks you to read user input in response to a question and store that input in a variable called response. So, call readLine on a console object, passing in the question as a parameter. Assign that into the variable called response.

That looks like:

String response = console.readLine("Do you understand do while loops?");

Next you want to set up a loop to continuously prompt the user while the response is "No". It also says to declare the response outside of the loop. Let's start with that, and the shell of a loop:

String response;
do{
  response = console.readLine("Do you understand do while loops?");
}while(**the answer is "No"**);

So, this loops constantly until the answer is not "No". How to test for that? We need to compare the contents of response with the string "No". We use the equals() method to test for string equality. So the condition for your loop could look like:

String response;
do{
  response = console.readLine("Do you understand do while loops?");
}while(response.equals("No"));

Lastly, you just need to create a line to output a result; I'll leave that with you.

I hope that made sense!

Steve.

No problem! :+1: :wink: