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

Meenakshi Bohra
Meenakshi Bohra
1,182 Points

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

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

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String inputFromUser;
do{
  inputFromUser = console.readLine("Do you understabnd the do/while Loop?  ");
  String response=console.readLine("  ");

 // "yes" or "no" is stored in the inputFromUser variable 
// when the user types "no" the loop repeats,  when  "yes" the loop is over
}
while(inputFromUser .equalsIgnoreCase("no"));

1 Answer

Hi there,

Your code was basically OK.

From the first task, where you were asked to store a response in the variable named response, that variable needs using throughout the rest of the code. So, I've switched inputFromUser to response. The second line redeclaring response isn't needed; I've commented that out. I also corrected the queestion being asked of the user, as I think the compiler is picky and wants that exactly as in the question.

Lastly, I removed the space before the dot notation in .equalsIgnoreCase and ended that line with a semi-colon. I've left the comparison as equalsIgnoreCase() although equals() works just fine too.

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

 // "yes" or "no" is stored in the inputFromUser variable 
// when the user types "no" the loop repeats,  when  "yes" the loop is over
}
while(response.equalsIgnoreCase("no"));

That corrects your code and, with just a few adjustments, what you had already done was pretty much there.

Steve.