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

Yacine Freifer
Yacine Freifer
6,717 Points

boolean error java

Can someone tell me why my code is not going through? the error seems to point to the semi colon after declaring the boolean statement

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

if (isInvalidWord); {
console.printf("Try again");}
}
while   (isInvalidWord);

4 Answers

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hello Yasine,

Remember DRY principle about repeating yourself, well Don't Repeat Yourself.

Once you have declared response as String outside the do-while loop you only need to initialize response inside the do-while loop but without using the keyword String.

boolean isInvalidWord should also be declared outside the do-while loop but initialized inside the do-while loop so that the do-while loop access to this boolean.

With do-while loop the code will run at least once and test if the condition has been met. If condition has not been met (i.e. FALSE) the code keeps looping until the condition becomes TRUE.

Please refer to the below code sample and let us know.

Part One

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

Part Two

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

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

Part Three_

String response;
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);
Yacine Freifer
Yacine Freifer
6,717 Points

Thank you, look forward to meeting you more on my journey good friend :)

Tonnie Fanadez yep, but in his example there were an extra opening bracket and he mentioned he had a compile error that place. Anyway, it does not matter now, good thing his problem is solved:)

There is a closing bracket missing there (or, actually, there is no need for the opening bracket after the equals sign).