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

logic of do while loops

I'm stuck on a very simple question and getting lots of errors. Is the issue with improper use of the String.equals the logic of my loop?

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

String.equals or the logic* I meant to say.

2 Answers

Ronald Williams
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ronald Williams
Java Web Development Techdegree Graduate 25,021 Points

First of all, I can reassure you that do while loops are not that simple when first learning programming. It just takes some time and with enough practice you will be able to master them! Here is one way to solve the challenge and why that code did not compile.

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

It is just a matter of separating your do and while. A do while loop will be as follows:

do {
     statement(s)
} while (expression);

Converted to an answer - Dane E. Parchment Jr. (Moderator)

The usage of equals method is correct, the problem is the usage of do-while loop. You clearly didn't understand the logic behind do-while loops, click on the link below, it will explain to you how do-while loops work compared to while loops and also there is an example , hope this helps! :)

Do-while loop vs while loop

Converted to an answer - Dane E. Parchment Jr. (Moderator)