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 trialBen Cavolick
371 Pointscode is correct
I'm getting an error with the below code, but I think it's correct:
String response;
boolean isInvalidResponse; do { response = console.readLine("Do you understand do while loops?"); isInvalidResponse = (response.equalsIgnoreCase("No")); } while(isInvalidResponse);
console.printf("Because you said " + response + ", you passed the test!");
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean isInvalidResponse;
do { response = console.readLine("Do you understand do while loops?");
isInvalidResponse = (response.equalsIgnoreCase("No"));
} while(isInvalidResponse);
console.printf("Because you said " + response + ", you passed the test!");
5 Answers
Joseph Sworyk
7,879 Pointsdo{
response = console.readLine("Do you understand do while loops?");
}
while(response.equalsIgnoreCase("no"));
console.printf("Because you said %s, you passed the test!",response);
Dane Parchment
Treehouse Moderator 11,077 PointsYour code is correct, but you are using printf
wrong. Remember that printf can format the text with variables. So you would write the end result as:
console.printf("Because you said %s, you passed the test!", response);
Ben Cavolick
371 Pointsindeed that's exactly what I was trying it to do.
Ben Cavolick
371 Pointsthis was in response to challenge 3 of 3 of Java basics "Finally, using console.printf print out a formatted string that says "Because you said <response>, you passed the test!""
- just needed to add a console.printf with the variable in the middle of the text, which I think I did correctly, do you think this is a bug with the editor or did I do something wrong with my code?
Dane Parchment
Treehouse Moderator 11,077 PointsNo his printf was wrong, the way he performed equalsIgnoreCase was also correct, the code was just expecting proper usage of printf.
Joseph Sworyk
7,879 PointsSo to get a clear idea of what you're trying to do... you want the loop to continue if response.= ' no'. But terminate and print to the console if response = 'yes' or anything but 'no?
Ben Cavolick
371 PointsBen Cavolick
371 Pointsbrilliant thanks! (it worked) but I thought I could add variables in between printf output text with a + is this not correct?