Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Looping until the value passes

taskNumber errors?

When I run the attached code I get the following error messages:

JavaTester.java:102: error: while expected } ^ JavaTester.java:125: error: illegal start of expression taskNumber = 1; ^ JavaTester.java:125: error: ')' expected taskNumber = 1;

I've tried googling these but haven't had much success in finding an explanation for what they are referring to. What is taskNumber and how can I remove these errors from my code? Also I feel like there is a way to avoid using two booleans for this exercise (i.e. a way to say that isBanana is false) but haven't learned this yet. Any ideas?

KnockKnock.java
    console.printf("Knock Knock.\n"); 
    String who = console.readLine("Who's there?  "); 
      boolean isBanana;
      boolean isOrange;
      do { isBanana= (who.equalsIgnoreCase("banana")); 
          if(isBanana) {console.printf("%s who?\n", who);
                        console.printf("Knock Knock. \n");}
          while(isBanana); 
           isOrange = (who.equalsIgnoreCase("orange")); 
          if(isOrange) {console.printf("%s who? \n", who); 
                        console.printf("Orange you glad I didn't say Banana again?");
                       }
          while(isOrange);
         }
Jorge Flores
Jorge Flores
Courses Plus Student 7,864 Points

hi Patrick i would suggest to just wrap this lines in the do while loop console.printf("Knock Knock.\n"); String who = console.readLine("Who's there? "); console.printf("%s who?\n", who);

and complete the condition asking if the String who is "banana" remember that you might have a problem if you dont declare the variable who outside the loop because then the condition will not acknowledge the variable, also remember to ignore case on the condition. You will have something like this

String who; do{ console.printf("Knock Knock.\n"); who= console.readLine("Who's there? "); console.printf("%s who?\n", who);

}while(who.equalsIgnoreCase("banana"));

Also remember to read carefully because the punchline is only printed on the second step of the challenge, and you will substitue the word Orange with the variable who, you will have something like this:

console.printf("%s you glad i didnĀ“t say banana again?", who);

concerning to your code, you have a few mistakes on the comprenhesion of the problem, you need to keep asking the person untill he answers "banana", then you leave the loop. Good Luck, hope i was of any help.

Thanks for your answer, fixed!

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

To answer your question about taskNumber, somehow it broke our internal TestRunner that was checking your code. Sorry for the confusion!