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

!How do you progress if you don't have the correct syntax for an exercise? Waisting my time now...

THis is RIDICULOUS !!!

I had trouble with this also. This is how I solved it. I hope this helps you.

String who; 

boolean repeat;

do {

console.printf("Knock. Knock.  \n");

who = console.readLine(Who's there?  ");

console.printf("%s who?", who);

repeat = (who.equalsIgnoreCase("banana"));

if (repeat) {

console.printf("try again.");

}

}while (repeat);
Ken Alger
Ken Alger
Treehouse Teacher

Nathan -

I edited your post for markup. Please see this post for information on how to post code in the forum.

Thanks!

Ken

11 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dolores;

Let's walk through this challenge together.

Task 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.

Here is the code and comments we are 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);

For task 1 we need to create the do...while loop and meet the task requirements. Let's look at this with some basic problem solving methods (Prepare, Plan, Perform, and Perfect). We'll likely leave the Perfect stage until later.

Prepare

We have watched the video lessons, and have the tools we need, we have the same output for which we are aiming in the code comments.

Plan

Here is what we are asked to do broken down into steps:

a. Move the prompting code into a do... while loop.

b. Check to see if the variable who is equal to banana.

c. They gave us a hint/tip of moving the who declaration outside the do block.

Perform

Our code would then need to look something like:

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

Does that make any sense?

Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dolores;

First, welcome to Treehouse and thanks for posting on the forum.

I understand how frustrating coding can be. Can you be a bit more specific as to where you are having a challenge and I am sure someone here on the forum will be able to assist.

Ken

This is how I programmed it and it will not satisfy the request to print - "Are you glad I did not say banana, supposedly after the do while completes..

How do I do it, so I can move on -just stuck and fustrated...

String who;
boolean banana;
do {
  console.printf("Knock Knock.\n");
  who = console.readLine("Who's there?  ");
  banana=(who.equalsIgnoreCase("banana"));
  console.printf("%s who?\n", who);
  if (banana) {
    console.printf("% are you glad I did not say banana\n",who);
  }
} while (banana);

Yes so far......How do we print outside of the Do While?

and thanks for getting back to me... Much appreciated.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dolores;

For Task 2 you need to do a console.printf line that matches the punchline code provided. This would be outside of the do... while block, so after all of the other code written.

You need to use the string formatter in the answer as well, so it would look like:

console.printf("%s you glad I didn't say Banana again?", who);

Ken

Thank you ... Much appreciated... just was so frustrated that I couldn't move on..

Now the way I did it, just to see if I understand this correctly.

Does the Boolean store a 1 if the condition is met or a 0 and then, for the IF statement with it, does it do the console printf - if the object is a 1 ?

Example:

if (banana) { console.printf("% are you glad I did not say banana\n",who); }

What does this If statement actually read as?

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dolores;

Well, to be honest using a Java Boolean condition for this challenge didn't enter into my mind. I have seen several other questions on the forum where a Boolean value for banana has been tried, but to me that makes it more complicated than is necessary.

To answer your question though, let's assume that in your example banana is a Boolean value. A Boolean value is one with two choices: true or false, yes or no, 1 or 0. If banana was valued as true, or 1, it would translate into English as:

If true print this message "...".

Getting banana as a Boolean is doable, see some of the ways in the above reference, but is, in my opinion, a bit overkill for this challenge.

Hope it helps and again, welcome to Treehouse.

Ken

I understand, the interaction with you on my questions make this a great learning experience....Key to success.

Wait, I am still getting an error.. It asks for Task To do the Print out after the do while loop and it doesn't recognize my console.printf command?

I thought I was done, but the computer lost connection and when it reconnects, it puts me back at the beginning of the task and I am once again not being able to go forward because its says I did not do task2 - its not regonizing my answer.

Here is my whole answer:

String who; boolean banana; do { console.printf("Knock Knock.\n"); who = console.readLine("Who's there? "); banana=(who.equalsIgnoreCase("banana")); console.printf("%s who?\n", who); if (banana) { console.printf("% are you glad I did not say banana\n",who); } } while (banana); console.printf("% you glad I didn't say Banana again", who);

I see now that you look for exact match to be able to move on:

I type this :

My answer : console.printf("% you glad I didn't say Banana again", who);

and it received the Bummer comment that I didn't do it

When I retype it adding in the "?" it was then successful.

From You: console.printf("%s you glad I didn't say Banana again?", who);

Only difference between the two was "?" = to be able to move on - that is a little tough.. but I did learn a lot.

Ken Alger
Ken Alger
Treehouse Teacher

Dolores;

Yes, the challenge checking engines can be rather persnickety on syntax and output. It is a great lesson to learn early on as it has caused much frustration in my experience as well.

Very pleased it all worked out. Hopefully your frustration level with Treehouse is reduced and you stick with it!

The other, hopefully helpful, tidbit I would offer is to try to break the challenge tasks down into smaller chunks, and work through them step by step. They really aren't designed to trick us or to make assumptions on our knowledge. If you watch the videos and work through the modules you generally are given the necessary information to complete the challenges.

Best of luck and happy coding,

Ken

For anyone else getting stuck with this, although I know this has already been answered, it seems as though MOST people will hit up the forum when they have tried and tried and tried to figure out where their mistakes are, and the compiler is not giving you the answers you need to fix your errors. So most of us use the forum as a last resort kind of thing. At least I do. I know that if I am always getting the correct answers solely from the forum and not entering the code myself, then I am not learning or retaining anything, I am just trying to get past the challenge and at least for me, that is not my goal. I actually want to learn coding. Having said all of that, the following is the code I entered for challenge 1 and it worked for me:

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

^^^^ where I initially screwed up that code was putting a %s AFTER the who?, so I just added one where it didn't need it.

Then the 2nd challenge code I entered:

console.printf("%s aren't you glad I didn't say Banana again?", who);

I had no problems with the second part of the challenge. I do have to say that a lot of this new language and verbiage makes my brain hurt and the way the questions are formulated sometimes, do not make sense to me, but that is probably just me.

Hope this helps.

Good Luck!