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 trialfrankcastle
1,738 Pointswhile loops in Java, need another example
Cant see what is wrong with this loop. Not getting any syntax errors
// I have initialized a java.io.Console for you. It is in a variable named console.
//String response = console.readLine("Do you understand while loops?");
String response = "NO";
boolean responseInvalid;
do {
responseInvalid = response.equalsIgnoreCase("No");
if (responseInvalid) {
console.printf("Reenter response \n");
}
} while(responseInvalid);
2 Answers
Andrew Dummer
6,885 PointsAdd response = console.readLine("Do you understand while loops?");
to the inside of your while loop. Every time the loop repeats it will grab input from the user.
Charlie Thomas
40,856 PointsString response;
boolean responseInvalid;
do {
response = console.readLine("Do you understand while loops?");
responseInvalid = response.equalsIgnoreCase("No");
if (responseInvalid) {
console.printf("Reenter response \n");
}
} while(responseInvalid);
You need to make sure you get the user's input inside the loop because otherwise response will not change and always be no.