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

Stuck on Stage 3 challenge: can't figure out the do while loop...

Just can't figure out how to properly write this out... Feel like a dummy!

Hi Scott,

Can you post what code you've attempted?

Hey Jason,

Thanks for answering...

What I was working off of is this...

String who;
do {
  console.printf("Knock Knock.\n");
  who = console.readLine("Who's there?  ");
  console.printf("%s who?\n", who);
} while (who.equalsIgnoreCase("banana");

I think i just need a better explanation of the reason and placement of "do" "if" and "while"... I just get lost why things go where they go and why they are used.

Ken Alger
Ken Alger
Treehouse Teacher

Scott;

I see that you requested an answer from me and that others have provided some great information. Let us know if the "do" and "if/while" concepts are still muddy. They are, in my opinion, essential building blocks for much of Java and programming in general.

Ken

Hey Ken,

My problem is just I can't conceptualize it when a problem is presented to me. I understand it all going through all the videos, quizzes and some excersises but as soon as I am presented something like this, I just go blank and have no idea what to type. I know there needs to be a "do" and "while" etc. and I can remember the placement of them from the videos but for some reason I just can't get my brain wrapped around where to properly place and structure everything to get it to work properly... Guess it just takes more time.

Do you understand the code that you posted above? Let us know if you don't.

That code is correct except you're missing the closing parenthesis on the while condition.

As long as you read the code challenge instructions carefully I think they do a pretty good job of guiding you to the solution.

I think you'll get better at the placement of things as you gain more practice.

1 Answer

Scott, in this loop we look at the do statement as do this, while the answer is this(banana) execute this code. So in the challenge the do is print Knock Knock, while the person answers banana we display banana who. In the next stage they will incorporate the if statement that will change the reply if the person answers "Orange". the code should for this challenge be:

String who;
do {
  console.printf("Knock Knock.\n");
  who = console.readLine("Who's there?  ");

    }while(who.equalsIgnoreCase("Banana"));
      console.printf("%s who?\n", who);

Hope this helps, if seems as clear as mud then sorry I can try to explain again.

Thanks so much David!