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

Challenge Task 1, stage 3 Perfecting the Prototype..How can i use a do while loop when i know one solution "banana" help

console.printf("Knock Knock.\n");
String who;
boolean isValidAnswer;

do {
  who= console.readLine("Who's there?  ");
  isValidAnswer= (who.equalsIgnoreCase("banana"));

  if (isValidAnswer) = false {
    console.printf("please try another response.");
  }
}while (isValidAnswer) = false;
Ken Alger
Ken Alger
Treehouse Teacher

Edited for markup

Timothy;

Please see this post for how to post code to the forum.

Ken

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Timothy;

Welcome to Treehouse!

Let's take a look at this Challenge Task.

Task - Prepare

Read the comments and code below. We want to move that prompting code into a do while loop. Wrap the code into a do while and check in the condition to see if who equals "banana" so the loop continues. Remember to move your who declaration outside the do block.

Code given:

/*  So the age old knock knock joke goes like this:
        Person A:  Knock Knock.
        Person B:  Who's there?
        Person A:  Banana
        Person B:  Banana who?
        ...This repeats until Person A answers Orange
        Person A:  Orange
        Person B:  Orange who?
        Person A:  Orange you glad I didn't say Banana again?
*/

//Here is the prompting code
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there?  ");
console.printf("%s who?\n", who);

Plan

Let's take a look at what we need to do.

  1. Create a do... while loop for the prompting code.
  2. Check to see if the condition equals "banana" to continue the loop.

Perform

The task instructions remind us to move our who declaration outside of our soon to be do... while loop. So let's split that up first, I'll just be working with the prompting code and will drop all of the comments out for this discussion.:

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

That is functionally the same as the previous code, we are now able to declare a do... while loop and not worry about getting errors for redefining the string who as a string, over and over again. For our loop, we need to do something while a certain condition exists. In this case we continue to ask the same knock-knock joke as long as the answer is banana. For this challenge imagine that only two input options are possible, banana and orange.

The do... while loop looks like:

String who;

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

This code will, therefore, run everything inside the do{} block of code as long as (while) our inputted variable who equals banana.

Does that help at all?

Happy coding,

Ken

yes it does thank you.

got that one but this one...Print out the punchline, after the while loop completes, using the who variable