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

Manisha Gopala
PLUS
Manisha Gopala
Courses Plus Student 203 Points

How to have a while loop that continuously runs when the string variable response equals No?

I am not sure how to convert a boolean to a string

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do tou understand do while loops?");
Boolean No;
while(true)
response = response.equals(No);

1 Answer

James Vlok
James Vlok
17,059 Points
//Create a String to store the users response
String response;
// Keep asking the user for a response while he answers "No".
do{
                //Prompt user and store their answer in response
        response = console.readLine("Do you understand do while loops?");
//Check if the user answered "No" if they did answer "No" loop again.
}while(response.equals("No"));

console.printf("Because you said %s, you passed the test!",response);

The string's equals method compares the value of the string and returns a Boolean (true/false). for example:

String myStr = "Hello";
Boolean isHello = myStr.equals("Hello");   // isHello will equal true

Boolean isHello = myStr.equals("Good Bye");   // isHello will equal false

Also something to keep in mind is the string equals method is case sensitive.