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

How do I this?

I'm supposed to make a console.printf saying the the input was the correct answer. But I don't know how to do so in a loop.

String response;
boolean isInvalidResponse;
do { 
  response = console.readLine("Do you understand while loops?  ");
  isInvalidResponse = (response.equalsIgnoreCase("no"));

if (response = ("yes")) {
  console.printf("Because you answered yes, you passed the test");
}

} while(isInvalidResponse);

[MOD: edited code block - srh]

Hi,

While posting the question please mention the excercise/quiz/challenge. It helps a lot.

Also, please enclose your code inside 3 back-tics (`) and then language name. for details see Markdown Cheatsheet.

And lastly, if your question is answered then upvote and accept the answer to avoid the other users to consider that you still need help.

1 Answer

Hi Lachlan,

I don't think the 'success message' is inside the loop. The loop terminates when someone types something other than 'No', then you print the outcome using a formatted string. There's no need for booleans or if statements.

So, outside of the loop, after it, add your success message using the formatter to interpolate the response value:

String response;

do{
  response = console.readLine("Do you understand do while loops?: ");
}while(response.equals("No")); // edited; thanks Krishna

console.printf("Because you answered %s, you passed the test!", response);

Something like that should do it.

Steve.

Right, but i would like you to please confirm if the brackets for do-Block are correct.

I think it should be do {}, rather than do ().

You are quite correct - I shall edit my answer- thank you.

As you say, having a link to the challenge helps to be able to test the proposed code before posting it! :wink:

Thanks. The reason I couldn't provide a link, reply, or add the " ``````` " thingos to the question was because I was on mobile.

on mobile... never thought of that. :)

Backticks are a pain on mobile keyboards, I know. I've tried and failed miserably many times - and spellcheck changes them to dots half the time!

Glad you got fixed and you can move on. Most of all, I hope you understood the answer.

Good luck with your future Treehouse courses!

Steve.