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

Ben Cavolick
Ben Cavolick
371 Points

code 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!");

Example.java
// 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
Joseph Sworyk
7,879 Points
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);
Ben Cavolick
Ben Cavolick
371 Points

brilliant thanks! (it worked) but I thought I could add variables in between printf output text with a + is this not correct?

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Your 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
Ben Cavolick
371 Points

indeed that's exactly what I was trying it to do.

Ben Cavolick
Ben Cavolick
371 Points

this 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
Dane Parchment
Treehouse Moderator 11,075 Points

No his printf was wrong, the way he performed equalsIgnoreCase was also correct, the code was just expecting proper usage of printf.

Joseph Sworyk
Joseph Sworyk
7,879 Points

So 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?