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 trialRavi Maniam
Courses Plus Student 1,032 PointsCode Challenge: Don't understand the question.
I don't know what I'm doing wrong with the challenge. I've tried so many times.
Here is the URL :http://teamtreehouse.com/library/java-basics/perfecting-the-prototype/looping-until-the-value-passes
My code is:
/* 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?
...This repeats until Person A answers Orange
Person A: Orange
Person B: Orange who?
Person A: Orange you glad I didn't say Banana again?
*/
boolean Who;
do {
Who = who.equalsIgnoreCase("banana");
} while(Who);
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
and they said :
Bummer! Did you put the prompting code in a do while block that is checking to see if who.equalsIgnoreCase("banana")
?
Gloria Dwomoh
13,116 PointsHey there, yes as Jeff said, show us what you have tried so we can help. In order to correctly paste code in the forum check out the video on the right.
Ravi Maniam
Courses Plus Student 1,032 PointsThanks for the tip! This is my first forum post so I didn't know how to add code.
Gloria Dwomoh
13,116 PointsYou are welcome :)
2 Answers
Gloria Dwomoh
13,116 PointsWhat you have done is (I have added some comments for you below)...
/*changed the String to boolen, it didn't ask for that, it wants you to move
the given declaration outside, without creating a new one and remember the camelCase convention*/
boolean Who;
/*Below you are are setting "Who" with the boolean that results
from whether "who" is equal to banana or not (true or false)*/
do {
Who = who.equalsIgnoreCase("banana");
} while(Who); //If Who is true ..which means who is equal to banana the loop keeps running after the 1st time
//Then you print knock knock and ask the user to give you "who is there"
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
Basically your code will throw some errors if you were to run this in an actual editor. 1.You haven't initialized "who" by the time you have used it so... who is an undefined variable.
Other errors occur as well but as soon as it reaches the do loop the code will break due to that error.
I am explaining what you have done so far, maybe that will help you understand why your code is wrong it doesn't do what it is supposed to do.
The code given by Jeff is correct. I hope this explains things to you.
Ravi Maniam
Courses Plus Student 1,032 PointsBoth answers are good but I chose this as the best answer because it helped me understand more.
Jeff Busch
19,287 PointsHi Ravi,
If who is going to accept input from the user it will probably have to be a string. Part of the condition is checking the user input so that needs o be in the loop. Take the code below one line at a time and think about the logic involved.
String who;
do {
console.printf("Knock Knock.\n");
who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));
console.printf("%s you glad you didn't say Banana again?", who);
Jeff
Jeff Busch
19,287 PointsJeff Busch
19,287 PointsLet's see what you have tried.