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 trial

Java Java Basics Perfecting the Prototype Looping until the value passes

Enrique Bernal
Enrique Bernal
3,584 Points

I 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
STAFF
Ken Alger
Treehouse Teacher

Luis;

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
Enrique Bernal
3,584 Points

You are the one!

I was thinking that first 3 lines must be used at the beginning the code, a little tricky for beginners.

Thanks

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Luis;

I would recommend reading this forum post.

Ken

Enrique Bernal
Enrique Bernal
3,584 Points

thanks 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
Ken Alger
Treehouse Teacher

Can you post the code you are currently using?

Ken

  1. 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);
  1. 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
Enrique Bernal
3,584 Points

Connor, 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
Enrique Bernal
3,584 Points

thanks Connor the challenge was done correctly