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 trialEnrique Bernal
3,584 PointsI have been trying to do the challenge without any success.
I can't finish the challenge since I am receiving an error when I hit check button. This is the only challenge I can't finish for this error. Error communication. on the other hand I can not figure out how this code must be done. any ideas?
Challenge Task 1 of 2
Read the comments and code below. We want to move that prompting code into a do while loop. Wrap the code into a do while and check in the condition to see if who equals "banana" so the loop continues. Remember to move your who declaration outside the do block.
3 Answers
Ken Alger
Treehouse TeacherLuis;
You need to delete the first three lines of code that "came with" the challenge.
You only need from String who
to the semi-colon at the end of the while
statement.
Ken
Enrique Bernal
3,584 Pointsthanks Ken, if I follow the code I got:
JavaTester.java:102: error: variable who is already defined in method run() String who = console.readLine("Who's there? "); ^ 1 error
/* 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?
*/
//Here is the prompting code
console.printf("Knock Knock.\n");
String who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
String who;
do {
console.printf("Knock Knock.\n");
who = console.readLine("Who's there? ");
console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));
Ken Alger
Treehouse TeacherCan you post the code you are currently using?
Ken
Connor Kuehl
4,393 Points- Did you place the code block inside of a do-while loop properly? This pseudocode should guide you in the right direction.
do {
code block here
}
while (conditional);
- If your do-while block is correct, did you remember to move the String declaration for your who variable outside of the do-while loop? This is likely the cause of the problem.
Enrique Bernal
3,584 PointsConnor, i follow your suggestion but without any success.
JavaTester.java:101: error: variable who is already defined in method run() String who; ^ 1 error
Enrique Bernal
3,584 Pointsthanks Connor the challenge was done correctly
Enrique Bernal
3,584 PointsEnrique Bernal
3,584 PointsYou are the one!
I was thinking that first 3 lines must be used at the beginning the code, a little tricky for beginners.
Thanks