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

Sunny Wang
PLUS
Sunny Wang
Courses Plus Student 314 Points

Looping until the value passes code problem

I've been trying this code problem for some time now but it just doesn't work. Here are the compiler errors: JavaTester.java:123: error: while expected } ^ JavaTester.java:126: error: illegal start of expression while (response.equalsIgnoreCase("no")); ^ JavaTester.java:126: error: ')' expected while (response.equalsIgnoreCase("no")); ^ JavaTester.java:126: error: ';' expected while (response.equalsIgnoreCase("no")); ^ JavaTester.java:126: error: illegal start of expression while (response.equalsIgnoreCase("no")); ^ JavaTester.java:126: error: ';' expected while (response.equalsIgnoreCase("no")); ^ 6 errors

appreciate any help!

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = "";
String respond;
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);

3 Answers

Christopher Augg
Christopher Augg
21,223 Points

Hello Sunny,

Good job! You almost got this one. Just some small errors:

       String response = "";
       //String respond;  You do not need this line.
       do { 
            response = console.readLine("Do you understand do while loops?");
       }
                    // } to many braces
       while (response.equalsIgnoreCase("no"));
       console.printf("Because you said %s, you passed the test!",response);

Hope this helps.

Regards,

Chris

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Sunny,

do-while loop is easy to understand. The loop will do what you declare in the do section, until your while section gets a different input then you want. But it will go through your do-section at least ones . So it will print out your text at the beginning.

Here a little not very funny example :)

Imagine, that you are ordering ice and the iceman asks you, whether you want ice. He will ask you "Do you want ice" until your input will be something other then "Yes" or "yes".

       String response = "";
       do { 
            response = console.readLine("Do you want ice?");
       }while (response.equalsIgnoreCase("Yes"));

The output should look like this:

"Do you want ice?"
Yes
"Do you want ice?"
yes
"Do you want ice?"
Yes
"Do you want ice?"
no
//the result of the while-section isnt Yes or yes, so it is false and the compiler goes out of the loop.

I hope it helps :)

Bon appetit

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