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 trialRichard Hewat
750 PointsI am having difficulties getting past the Java Basics knockknock.java challenge. Keeps telling me to try again.
My code does not have compiler errors - but the code check fails and I am forced to try again. I can run the code in my workspace. What do I need to do to get past this step?
import java.io.Console;
public class Joke {
public static void main(String[] args) {
Console console = System.console();
String who;
boolean isBananaWho;
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\n\n", who);
isBananaWho = (who.equalsIgnoreCase("banana"));
}
while (isBananaWho);
if
(who.equalsIgnoreCase("orange"));
console.printf("%s you glad I didn't say Banana again?\n", who);
System.exit(0);
} }
/* 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;
boolean isBananaWho;
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\n\n", who);
isBananaWho = (who.equalsIgnoreCase("banana"));
}
while (isBananaWho);
if
(who.equalsIgnoreCase("orange"));
console.printf("Orange you glad I didn't say Banana again?\n");
System.exit(0);
// ==== END PROMPTING CODE ====
2 Answers
Richard Hewat
750 PointsYep - the syntax was not quite right. Changed my IF statement to look like this:
if ( who.equalsIgnoreCase("orange") ) { console.printf("%s you glad I didn't say Banana again?\n", who); System.exit(0);
...and runs fine now. Thanks for your speedy help. i have now completed the Java basics and hope to start the Java Objects tutorial tomorrow.
Regards,
Richard
Craig Dennis
Treehouse TeacherAre you missing a closing curly brace for your if? If not the System.exit might be messing up the runner.
Your logic looks great!