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 trialshiny peanut
5,113 PointsLoop Confusion
I'm a bit confused about how to best answer this question. Is it necessary to create a second variable? Am I misunderstanding the logic/flow of control for this problem? Any help is appreciated.
// 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? \n\n");
boolean answer;
do {
response = console.readLine("Do you understand while loops? \n\n");
answer = (noun.equalsIgnoreCase("no"));
if (answer) {
console.printf("Okay. \n\n");
}
} while(answer);
2 Answers
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsYou don't need to have this line outside of the do-block because the variable is only used inside the do-block
String response = console.readLine("Do you understand while loops? \n\n");
Also, this line would cause some kinda problem I guess, because the variable noun has not been declared anywhere
answer = (noun.equalsIgnoreCase("no"));
Philip Schultz
11,437 PointsI see a couple things that need fixing. You should just initialize the variable 'response' outside the do-while loop (set it equal to an empty string). You are asking for the input correctly within the loop, but you are testing some variable name 'noun' that doesn't exist. You want to test the response variable. See the code below on how I solved the task and let me know if you have any questions.
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = "";
boolean tester = true;
do{
response = console.readLine("Do you understand do while loops?");
if (response != "No"){
tester = false;
}
} while (tester);
console.printf("Because you said %s, you passed the test", response);