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

stuck on the the do loop keeps giving me a compile error not sure whats worng?

You have my code there if someone can take a look and let me know where in my logic i have it wrong will help me greatly

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

                        if (isInvalidResponse)
                          console.printf( "please input again"); 
                        }
                        while (isInvalidResponse); 

Hey! I just did the challenge, and figured it out. You are missing an initial curly brace { after the if statement, and you need one more ending curly brace } right before your while loop. Hope this helps! :)

My code

String response; 
boolean isInvalidResponse; 
do {
    response = console.readLine("Do you understand do while loops?"); 
    isInvalidResponse = response.equalsIgnoreCase( "no" ); 

                        if (isInvalidResponse){
                          console.printf( "please input again"); 
                        }
}
while(isInvalidResponse);

2 Answers

Hi guys,

Just a pointer on this challenge - you seem to have working code. However, you can simplify this.

The while loop takes a condition and loops if that is true. The condition we're testing here is whether response contains "No". If it does; loop - if not, don't! That's what while loops do. What are we looping? That's just the prompt for the new response.

There's no real need for booleans and if tests - there's only one test required and the loop can manage that for us. Have a look at:

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

You can use equalsIgnoreCase if you like, but this meets the requirements of the challenge. We set up the response variable, and enter the do/while loop, prompting for an answer and storing that in the response variable. We reach the end of the loop and test the answer provided. If it is "No", we loop again and repeat the above. Otherwise, the loop exits.

I thought I'd point out that there's no needs to run more than one test in this challenge; the loop can handle that for us without getting if involved.

Steve.

Ahmed Abbouh
Ahmed Abbouh
17,034 Points

hi, try to always indent your code for more readability and for fast debugging. Good coding!