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

Ben Mair
Ben Mair
8,728 Points

What is the illegal start here?

getting an illegal start

KnockKnock.java
/*  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?
    ...as long as Person A has answered Banana the above repeats endlessly
    ...assuming the person answers Orange we'd see
    Person B:  Orange who?
    ...and then the punchline.
    Person A:  Orange you glad I didn't say Banana again?
    (It's a really bad joke that makes it sound like "Aren't you glad I didn't say Banana again?")

    Let's just assume the only two words passed in from the console from Person B are either banana or orange.
*/

// ====BEGIN PROMPTING CODE====

// Person A asks:
console.printf("Knock Knock.\n");
String who;
  do {
// Person B asks and Person A's response is stored in the String who:
  who = console.readLine("Who's there?  ");

  if (who.equalsIgnoreCase(banana)); {
    // Person B responds:
  console.printf("%s who?\n", who);
  } while (who.equalsIgnoreCase(orange));
  console.printf("%s who?\n", who);


  console.printf("Orange you glad I didn't say Banana again?");
  }

// ==== END PROMPTING CODE ====

4 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Ben,

Just a few things need adjusted in this. The first is that there should not be a semi-colon to end your if statement, that is always done after the code that is run if the condition is true is complete.

Also the reason for the error is for where you placed the last closing bracket, We want the while loop to finish before the console prints "Orange you glad I didn't say banana" so the do loop always ends before the while clause.

Also be sure that when passing your String.equalsIgnoreCase statements in your control flow you are putting quotatins around the string you want to be the conditional.

The syntax of it looks like like this.

do {
//code to repeat 

} while(//statment that can break the loop);

Also, you are having the statement check to see if the answer equals orange, when it does you're asking your loop to continue to go through again, since Craig said assume that only banana or Orange will be passed into the console, it's fine to write your while statement as

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

It should look something like below.

  String who;
  do {
// Person B asks and Person A's response is stored in the String who:
  who = console.readLine("Who's there?  ");

  if (who.equalsIgnoreCase("banana")) {
    // Person B responds:
  console.printf("%s who?\n", who);
  }
  } while (who.equalsIgnoreCase("banana")); 


  console.printf("Orange you glad I didn't say Banana again?");

Thanks, let me know if this doesn't help and I'll try to look into it more.

Did you check your program Mr Rob. if you do not test for oranges and in the next stage and do as you proposed yo get the following error

Bummer! Did you forget to call `console.printf` with a format string of "%s you glad..."?

Hence i recommend testing for oranges as i did below. For the first one you can do as follows. String who;

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

but for the second task you can use the if statement as follows

if (who.equalsIgnoreCase("orange")){
console.printf("%s i am glad you are not another Banana", who);
}
Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there,

I'm sorry I thought the question was for passing the first part, to pass the second you can keep the above code the same and just add an %s in your printf. I'm just hesitant because the challenge asked in the directions to check and see if the answer was equal to apple and continue the loop if so. But I'm glad you had it work.

Ben Mair
Ben Mair
8,728 Points

I figured it out........Thanks everyone

Ben Mair
Ben Mair
8,728 Points

Thanks Much.......That probably will be used! :-) I am new to programming and am trying to learn Java on my own.......It has been an adventure this far!

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

I'm glad to hear you've started to pick it up and enjoying it, Java is one of those languages that is more strict and rigid in it's syntax, the good news is that it develops really good programming habits, so you chose the right language to start with!