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

Nicholas Cawthon-Church
Nicholas Cawthon-Church
535 Points

Do While.

I believe that I have this all wrong if someone could help to correct me, again. thanks!

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
console.printf("Do you understand do while loops?");
String response;
boolean yesAnswer;
boolean noAnswer;
do { response = console.readLine("Enter yes or no");
     yesAnswer = (response.equalsIgnoreCase("yes");
         if (yesAnswer) {
         console.printf("Good job!!");  
         }
         if (noAnswer) {
         console.printf("Dont give up!!");
         }
                } while(yesAnswer);

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Nicholas,

look at my code suggestion:

// TASK 1
// String response = console.readLine("Do you understand do while loops?");
// declare a String variable to store users input
// store the input into it using readLine method
// for TASK 2
// move the response assignment inside the do block
// so it can be done at least once
// leave the String declaration outside, so it is visible not only inside the block

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

// TASK 3

console.printf("Because you said %s, you passed the test!", response);
// call the prntf function and replace your response with %s

Does it make sense?

Grigorij

Kervin Bruce
Kervin Bruce
5,409 Points

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