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 trialYacine Freifer
6,717 Pointsboolean 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
// 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
UX Design Techdegree Graduate 22,796 PointsHello 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);
Adam Kubriczky
4,405 PointsTonnie 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:)
Adam Kubriczky
4,405 PointsThere is a closing bracket missing there (or, actually, there is no need for the opening bracket after the equals sign).
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsI think there is no need for closing and opening brackets since we are not inside a method
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 PointsYasine,
You're welcome, see you around.
Yacine Freifer
6,717 PointsYacine Freifer
6,717 PointsThank you, look forward to meeting you more on my journey good friend :)