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

Benjamin Smith
Benjamin Smith
547 Points

What am I doing wrong here....

Im not sure what im doing wrong :/

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?
    ...as long as Person A has answered Banana the above repeats endlessly
    ...assuming the person answers Orange we'd see
    Person B:  Orange who?
    ...and then the punchline.
    Person A:  Orange you glad I didn't say Banana again?
    (It's a really bad joke that makes it sound like "Aren't you glad I didn't say Banana again?")

    Let's just assume the only two words passed in from the console from Person B are either banana or orange.
*/

// ====BEGIN PROMPTING CODE====

// Person A asks:
console.printf("Knock Knock.\n");

// Person B asks and Person A's response is stored in the String who:
String who;
boolean isInvalidWord;
do {
  who = console.readLine("Who's there?  "); 
  isInvalidWord = (who.equalsIgnoreCase("banana"));
  if (who.equalsIgnoreCase = "orange") {
    console.printf("%s who?\n", who);
  }
  console.printf("orange you glad I didn't say Banana again?");
} while(isInvalidWord);
// Person B responds:


// ==== END PROMPTING CODE ====

3 Answers

Have a look at this forum post and my reply to it and see if that helps ...

If not, please shout!

https://teamtreehouse.com/forum/while-taking-this-challenge-should-i-still-insert-an-if-statement

You mixed it up Boss. U need not use the if statement inside the Do while loop. the if statement can be used in the second task not the first one. for the first one you can do as follows. String who;

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

but for the second task you can use the if statement as follows

if (who.equalsIgnoreCase("orange")){
console.printf("%s i am glad you are not another Banana", who);
}

The task assumes that there are only two possible entries made, orange or banana. So, once you've cleared the do ... while loop, there's no need for a further test. So, while who equals banana, continue looping, as soon as it doesn't equal banana, it must equal orange as there is no other possible answer - so you don't need to test for orangeness!

Hiya,

THe last line just needs to output the punchline with the %s inserted for the variable who.

console.printf(%s you glad I didn't say banana again!, who);

THere's no need for an if statement and, while it does no harm, in the context of the environment we're in it is an extra test that tells us something we already know. The loop exited because who wasn't "banana" - that means it is "orange"; there's no other possibility in the context of this challenge.

Steve.

Did you check your program Mr Hunter. if you do not test for oranges and in the next stage and do as you proposed yo get the following error

Bummer! Did you forget to call `console.printf` with a format string of "%s you glad..."?

Hence i recommend testing for oranges as i did

I shall check that - thanks for the pointer!

Steve.