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

do I have to declare a boolean in order to do this is exercise?

The question does say I have to but do I indeed need to declare a boolean for this exercise?

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
console.readLine("Do you understand do while loops");
String response = ("Not Really? ");
    do {
      response = console.readLine("No");
    }
} while response;
  console.printf("I'm having a hard time");

2 Answers

Giovanni Esposito
Giovanni Esposito
7,830 Points

Hi C.J. Doyle,

You'll need to use a boolean expression to the while stop condition. You can declare a boolean variable or doit directly on the conditional statement. For example:

Case a.-

console.writeLine("Do you understand do while loops"); String response = ("Not Really? "); boolean result = true; do { response = console.readLine("No"); result = ("No").equalsIgnoreCase(response); } while (result); console.printf("I'm having a hard time");

Case b.-

console.writeLine("Do you understand do while loops"); String response = ("Not Really? "); boolean result = true; do { response = console.readLine("No"); } while (("No").equalsIgnoreCase(response)); console.printf("I'm having a hard time");

I hope it is helpful. Remember vote for the answer

Thank you!

Antonio De Rose
Antonio De Rose
20,884 Points

I will just give you a start up, just give it a try

String response = ""// you do it outside the loop, not to declare a variable all the time

do{
//code comes here, dont forget the semicolon
}while()//boolean condition, comes inside the brackets, dont forget the semicolon