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

stage 3 challenge 1

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.

KnockKnock.java
/*  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);
 string who=console.readLine("banana");
do{
    if(who.equalsIgnoreCase(banana)||
       who.equalsIgnoreCase(orange));
    }
}while(who.equalsIgnoreCase(banana)

Hi,

I can see your mistakes but what are your exact problems? I only see you posting the exercises with your code but you don't state what your problem is. That way you won't learn anything good by just wanting to know the solutions.

l am stuck l have tried to answer with what l have learned but its not working

2 Answers

Alright, first, the prompting code is not within the do-while loop. Also your if statement is syntactically not correct. The semicolon shouldn't be within the evaluation of the condition. You forgot the opening curly brace for the if statement. But you actually don't need the if statement.

Within the do-while loop, put in the prompts when somebody answers "banana". Outside that loop, put in the prompts for the case when somebody answers "orange". The evaluation should be in within the braces of the while statement like you did.

I could write the correct code in here but I think one learns best when trying himself with some tips in mind :-). Try it, I am sure you will get it done yourself and if you still have problems just asked again.

Andrew Wiley
Andrew Wiley
27,097 Points

I don't under stand this either.... I'm confused. It doesn't seem like the one we did in the video

Hey Andrew Wiley I think that should be a good start:

String who = "";  // has to be outside the loop

do {

// all the prompt for the case "banana"
    console.printf("Knock Knock.\n");
    who = console.readLine("Who's there?  ");   
    console.printf("%s who?\n", who);

} while(who.equals("banana"));

// The rest should go here...

The handling for the case that one types in "orange" should then be outside the loop.

Can you do something with that?

thank you very much l have made it....thanks for the lesson

You are welcome MUZ140118 MAKUYA I am glad that I could help you :-). Really trying yourself + asking for tips and hints is the best way to learn. If you only get the answer you will have more and more problems the more advance the exercises will get.

Andrew Wiley
Andrew Wiley
27,097 Points

OOOHHHHH!!!!! Now I understand what I was doing wrong. Thank you so much for the help!

Andrew Wiley you are welcome :D.