Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Looping until the value passes

Kasey Domer
Kasey Domer
787 Points

I'm really stumped by this do while loop.

I'm getting syntax errors with this code (it's in a code challenge), and I'm not sure why. HALP! This is my first day learning Java, so sorry if it's really obvious. Lol

Example.java
// 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?  ");
  if (response.equalsIgnoreCase("No")) {
    console.printf("Please try again.");
  } while (response.equalsIgnoreCase("No"));
           }

2 Answers

Hi Kasey,

You've got everything you need in your answer, and a little bit more too!! As Jeremiah has said, you need to move some of your curly braces about but I think we can simplify the code a bit too. Take Jeremiah's simple example of a do-while loop:

do {
  // this code
} while (this is true);

What are the "this code" and "this is true" parts for our challenge? Well, the "this is true" bit is the user answering "No" - we want to loop while they enter the answer "No". So what do we want to do inside each loop? We want to prompt them for their response, again.

So, swapping out the placeholders above and adding your code for those parts (and declaring the response string) gives us:

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

I hope that makes sense and simplifies your answer to the challenge.

Steve.

Kasey Domer
Kasey Domer
787 Points

It did! Thank you! :)

In this case, it is the syntax that is causing the error. The WHILE statement should be outside of the DO loop.

DO {
//something
} WHILE (THISISTRUE);

I hope this helps.

Kasey Domer
Kasey Domer
787 Points

My "while" statement is outside the do loop, though, isn't it?

No your while statement is included. Here is the breakdown.

do { //open DO statement
  response = console.readLine("Do you understand do while loops?  ");
  if (response.equalsIgnoreCase("No")) { //open if statement
    console.printf("Please try again.");
  } while (response.equalsIgnoreCase("No")); //curly brace closes if statement
           }// closes do statement

All you need to do is move the last curly brace just before while so it'll look like this: }} while (...);