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

Tanguy Muffat
PLUS
Tanguy Muffat
Courses Plus Student 313 Points

do while loop - Java Basic

Hi guys,

I would need you support. I'm stucked under challenge 2/3 where I'm being asked: "Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop."

I entered the following but I iget a compiler error. Any Idea where I do wrong.

Thank you for your support. Tanguy

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean invalidResponse;

do {
response = console.readLine("Do you understand do while loops?: ");
invalidReponse = response.equalsIgnoreCase("no");

  if (invalidResponse) {
  console.printf("Sorry you cannot continue the training. Train harder to understand the concept. \n");
  }
} while(invalidResponse);

2 Answers

Hi Tanguy,

I have seen this problem come up a lot. You do not actually need a boolean to run your do while loop, and you can simplify it a lot by having the loop depend on the response directly.

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

You may also include your print statement that you included using if(response.equalsIgnoreCase("no")).

HOWEVER, with all that being said, what you did actually works fine! You just made a typo in your code. You wrote invalidReponse not invalidReSponse, as declared outside your loop. You can see these errors easily if you check the compiler error (You would see a symbol not found error point to that variable)

Hope that helps!

I think i'm very unfamiliar with this kind of Java syntax. Using the console.printf() instead of System.out.println() and console.readLine() instead of scanner.nextLine(). I still don't understand why Tanguy Muffat's program works after fixing the syntax error.

I don't understand how having the [while(response.equalsIgnoreCase("no"));] at the bottom works. Normally i would expect it to look more like : do{ while(response.equalsIgnoreCase("no")) { console.printf("Sorry you cannot continue the training. Train harder to understand the concept. \n"); }}

I would think that a while condition with no brackets under it and nothing inside of it wouldn't work.

In Tanguy Muffat's program it looks like the while loop is completely outside of the do loop.

Hi Che,

What you are seeing in Tanguy's code is a do while loop. This is different from a simple while loop, both in syntax and in function. In terms of function a do while loop will always run at least once, regardless if the conditional is true or not. A simple while loop will only run if the conditional is true.

while(false){
//I will never loop
}

do{
//I will still loop once
}while(false)

In terms of syntax, the conditional of a do while loop simply is written after the closing curly brace of the do portion of the loop, exactly as Tanguy had written it. Had Tanguy excluded the keyword do before writing the loop, you are correct in thinking the code would not compile.