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 trialvikster
1,991 PointsI don't quite understand this...could you explain the task in a different way maybe?
I am confused on what to do inside the do while loop and how to prompt for an answer afterward
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do{
console.readLine("Do you Understand do while loops?");
} while(response);
3 Answers
Sjors Theuns
6,091 PointsHi Viktoria,
the first task of this challenge is to simply store the value in a string called response, so there is no do...while loop needed at this point. Try:
String response = console.readLine("Do you Understand do while loops?");
The for the second task of this challenge you do need to use a do...while loop for checking the giving answer. Like so:
String response;
do {
response = console.readLine("Do you Understand do while loops?");
} while(response.equalsIgnoreCase("no"));
And finally for the last task, just add a simple console.printf statement:
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!", response);
I hope that you can understand what I wrote here. Because simply copying and pasting would pass the challenge, but it is also important to understand how it works ("Do you Understand do while loops?" ;-) )
vikster
1,991 PointsYes I understand! I was confused on why you put the response.equalsIngnoreCase directly after the while instead on within {} like you do with the "do" part of the loop.
Sjors Theuns
6,091 PointsHi Viktoria,
You can also do it inside the loop. For this you need to declare a boolean variable, see here:
String response;
boolean isInvalid;
do {
response = console.readLine("Do you Understand do while loops?");
isInvalid = response.equalsIgnoreCase("no");
} while(isInvalid);
console.printf("Because you said %s, you passed the test!", response);
This way the loop keeps going as long as the boolean value is true. In this case if you enter the word "no". The outcome will be the same as my previous example. They are just different approaches.
Hope this helps.