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 trialMax Reinsch
5,740 PointsLast Java challenge (1 of 2)
I'm struggling to fully grasp the question being asked.
I've tried countless times but can't quite understand where I'm going wrong.
My latest attempt is as follows:
boolean who;
do {
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
if (who.equals("banana")) {
String who = console.readLine("Who's there? ");} while (who.equals("banana"));
console.printf(who);
console.printf("%s who?\n", who);
console.printf("%s you glad I didn't say Banana again?")}
}
I know I've gone a bit off the rails with this particular attempt, so just pointing me in the right direction would be great.
2 Answers
Robert Richey
Courses Plus Student 16,352 PointsHi Max,
The first step is to setup a do while loop.
do {
// inside the loop
} while (condition);
So, with this code, we're trying to do something while some condition is true. Also, for this to work, the String variable who
needs to be declared above the loop. Below, I'm declaring the variable 'who' of type String to initially hold an empty string. This loop will continue so long as the variable 'who' holds the value 'banana', so let's place that condition inside the while's parentheses.
String who = "";
do {
// inside the loop
} while (who.equals("banana"));
Now, what needs to happen each time through the loop?
Craig Dennis
Treehouse TeacherMax,
The loop should take care of checking about the banana bit, the if
is unnecessary. Remember to use equalsIgnoreCase
. It also looks like you might have an odd number of closing parenthesis.
You're close!
Max Reinsch
5,740 PointsI did the following (and finally succeeded):
String who = "banana";
do {
console.printf("Knock Knock.\n");
who = console.readLine("Who's there? ");
} while (who.equalsIgnoreCase("banana"));
console.printf("%s who?\n", who);
It turns out the solution was a lot simpler than expected; I just didn't quite get the question.
Cheers Craig Dennis.
Craig Dennis
Treehouse TeacherCongrats Max Reinsch !!! New course dropping next week!
Max Reinsch
5,740 PointsBtw Craig Dennis, I have a request/recommendation.
How about making a list of projects for us to do, as in ask us to make a Java program/s to acheive a certain outcome. It would be a good way for us to get stuck in and experiment?
Cheers.
Max Reinsch
5,740 PointsMax Reinsch
5,740 PointsI had an issue with what the question was asking; got there in the end!
Thanks Robert Richey.