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

Luis Ignacio Gomez
Luis Ignacio Gomez
567 Points

I do not know:

How to tell the console "hey, if the answer is "no", keep runing without using an "isInvalidWord" loop. Also Idk how to put that a while loop.

I feel like I do not know nothing :(

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? ");
}

2 Answers

Ivan Penchev
Ivan Penchev
13,832 Points

The logic of do-while can be found here: https://www.tutorialspoint.com/cprogramming/images/cpp_do_while_loop.jpg

Basically you do something, atleast once and then check for the condition.

So lets see what you have? well you do indeed have what to do:

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

but you are missing the condition.

How can we add the condition?? Lets check the documentation, its located here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html The Java programming language also provides a do-while statement, which can be expressed as follows:

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

so we need a while? OK!

so what should the condition be? Lets read your requirements: "How to tell the console "hey, if the answer is "no", keep runing "

So what is the "console" its an special object that we use that gives us input in the 'response ' variable. So for our condition we must evaluate that

in short, the answer is this:

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

Be sure to vote this answer if it was helpful, or leave a comment if you have more questions.

Luis Ignacio Gomez
Luis Ignacio Gomez
567 Points

Thank you very much, your answer was very helpful! :)

Raffael Dettling
Raffael Dettling
32,998 Points

The while condition comes after the last } and use the equals methode to compare to strings and don´t use response == "NO" i made that mistake :) Everything else looks good ^^

}while(response.equals("No"));

Why you sould use equals => https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

Ivan Penchev
Ivan Penchev
13,832 Points

Thanks mate, nice catch with the equals method, I edited my answer because of you :) Been a long time since I used Java.

Raffael Dettling
Raffael Dettling
32,998 Points

You´re welcome :) I usually use the == operator too. Instead of using methods :D It´s way quicker