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

Can someone help me with this code challenge?

I really need help I spent too much time on this one one problem. So if someone could give me the answers to this code challenge it would be very amazing. http://teamtreehouse.com/library/java-basics/perfecting-the-prototype/looping-until-the-value-passes

I believe it is way better to show us your code so that we can help you see the mistakes than give you the answer right away. So try posting what you have worked on so far and the error you get if possible.

I deleted it all, so I can't CTRL + Z and or CTRL + Y no more;.

Re-try the challenge again then and if the code still isn't working post it here and we will help you correct it!

3 Answers

I'll assume you are in the 1st challenge as your code does not pass the 1st challenge.

What the task asks is...

1.To move the prompting code in a do-while loop 2.Take the declaration of who outside of the do-while block you created.

Your code creates a new boolean variable and I have seen this mistake often, I don't know where that boolean variable comes from as it does not ask for it.

It also asks to take the definition of "who" alone outside of the do-while block.

Which is the "String who" part.

Then it wants you to make sure that....

  1. Continue the loop as long as the input is equal to banana...

what you did...

"while (isInvalidWord); " is not necessary wrong...but it is not really needed to do as you can straight away pass the code as...

while(who.equalsIgnoreCase("banana"))

eventually doing all the changes I mentioned you will end up with something like this

String who;

do{
//Here is the prompting code
console.printf("Knock Knock.\n");
who = console.readLine("Who's there?  ");
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));

which is what it asks from you to do and it is short and simple. Now try to complete the 2nd task. Read it carefully.

Thank you, but how would I do number two?

By reading the question carefully, understanding what it wants you to do and try to shape your answer to match that. If you don't understand these problems, making application is harder than these so you have to make sure you put the effort to try solving them.

Could you please just give me the answer mate.

/*  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);
    boolean isInvalidWord;

      do 
        {

    who = console.readLine("Who is it? ");
    isInvalidWord = (who.equalsIgnoreCase("banana") 

      if (isInvalidWord)
      {
        console.printf("Try to input banana \n\n");
      }

     }

      while (isInvalidWord);

I markdown your code better so others can see it better as well. If you will like to learn how to do that look at the video on the right "Tips for asking questions".

I just want the answer so I can continue and make an application please.

The problem is not the answer, if you don't understand how to come up with the answer then continuing with questions will make things hard for you to understand, so please take your time and try to solve, understand the challenges before you move on. The point is not passing the challenge, the post is understanding how the solution came about. That is the most valuable thing.

Someone please help me with question number two!

Ken Alger
Ken Alger
Treehouse Teacher

Leon;

Did you get this one sorted out?

Ken