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

I'm stuck in the last task. I'm working on the banana joke

Hello. Please help me. I don't know what I did wrong.

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

7 Answers

Nicolas Hampton
Nicolas Hampton
44,725 Points

You declare the who variable twice. Only declare it outside the loop, then initialize its value without declaration inside, so it has the scope it needs. You're trying to use the double equals to test for equality between two strings. I don't think this will work. Use the equalsIgnoreCase() method in the String class to compare two strings like I said in the first comment. That should clear your compile errors. As for the goal of the exercise, I find their implementation of the knock knock joke a little confusing, so no help there.I think that took a bit of luck to find what they're looking for, but I think that should work. All in all, the code should look like this, from what you show me:

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

happy coding! (Again, lol) Nicolas

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

is I believe what Adam is trying to show you. The do/while loop allows us to go through the code inside once before performing the test to break out of the loop. equalsIgnoreCase() is a method that compares to strings, a method of the String data type, and all the methods for Strings are listed in the java documentation at: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

you should get used to looking those up on that site, if you type java whateverMethodYourLookingFor in a google search, the oracle documentation is usually the first result, extremely helpful. Also, Adam Bell, if you do a return and type three backticks (Shift+the key next to the number one) followed by java, put your code after, then finish it with three backticks and a return, you can get that code format window you're looking for. Remember to make it an answer instead of a comment so you can get the points for it, too! Happy coding!

Nicolas

Hi Nick. I did. :)

Ok, thanks Nicolas. I'll try the code again.

java // ====BEGIN PROMPTING CODE==== String who; do { // Person A asks: console.printf("Knock Knock.\n"); // Person B asks and Person A's response is stored in the String who: String who = console.readLine("Who's there? "); // Person B responds: console.printf("%s who?\n", who); } while (who == "banana"); who = "Orange"; console.printf("%s you glad I didn't say Banana again?", who); // ==== END PROMPTING CODE ====

Nicolas Hampton
Nicolas Hampton
44,725 Points

I think you still missed the backticks Adam. I can't write them here because they'll be translated by the text window and I don't know the escape, but let me try:

Code Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

        ```html
        <p>This is code!</p>
        ```

Took Anyways, all the instructions for that is in the link "Markdown Cheatsheet" at the bottom of these text bubbles. I had to cut and paste the instructions from there. Comes in handy if you can figure it out. Also, the little eye in the lower right hand corner of the text bubble will let you preview it before hand.

// ====BEGIN PROMPTING CODE====
console.printf("Knock Knock.\n");
String who;
boolean isInvalidWord;
do {
  console.readLine("Who's there?  ");
  isInvalidWord = (noun.equalsIgnoreCase("banana"));
  if (isInvalidWord) {
    console.printf("%s who?\n", who);
} while (who.equalsIgnoreCase("banana"));
who = "Orange";
console.printf("%s you glad I didn't say Banana again?", who);
// ==== END PROMPTING CODE ==== ==== END PROMPTING CODE ====

Hey I got it! Finally.... But I'm still stuck on this code. I've tried everything I can think of.

OMG Dude, you're awesome. It worked. Thanks for reminding me that I was naming the String twice.

Nicolas Hampton
Nicolas Hampton
44,725 Points

Great, it worked! Mind marking it best answer? It would help me out a lot! Congrats!

Nic