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 trialDaniel Schwemmlein
4,960 Pointsstuck at the last intro to java challenge
Hi. Think I made a couple of stupid mistakes here. Can any of you guys help me out? Got confused because what I learned in the last video was a little different from what is asked here in this challenge. Unfortunately it won't show me where my errors are in the preview thats why i have to reach out to you guys
/* 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====
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? ");
if who.equals("banana")) {
console.printf("%s who?\n", who);
}
} while who.equals("banana");
// Person B responds:
// ==== END PROMPTING CODE ====
2 Answers
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHey There Daniel,
Just a few things need to change with this code.
When declaring a variable a String, since String is a class built into java we always capitalize it, this is different than int or byte because those are primitive types.
Secondly and really the only thing that needs to change, remember in if statements or while statements the condition to check for needs to be in parenthesis.
Overall this was very well done, just a few minor errors that we all make when we start.
The below code should help this pass.
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? ");
if (who.equals("banana")) {
console.printf("%s who?\n", who);
}
} while (who.equals("banana"));
Thanks feel free to respond if this doesn't help or if you have any further questions, I'll be happy to help.
ISAIAH S
1,409 PointsFor the second task, this code should pass.
// ====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? ");
// Person B responds:
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));
console.printf("%s you glad i didn't say banana again?", who);
// ==== END PROMPTING CODE ====