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

can you insert 2 boolean simultaneously? e.g boolean isBanana; boolean isOrange;

// i wonder if you have to boolean simultaneously e.g. String noun; boolean isBanana; boolean isOrange; do { console.printf("Knock Knock. \n"); who = console.readLine("Who's there? \n"); isBanana = (who.equalsIgnoreCase('banana'")); isOrange = (who.equalsIgnoreCase("orange")); }while(isBanana); if (isOrange) {

console.printf("%s who? \n"); console.printf("%s you glad i didn't say banana anymore? \n"); }

//is something like that do able?

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====
String noun;
boolean isBanana;
do {
// Person A asks:
console.printf("Knock Knock.\n");

// Person B asks and Person A's response is stored in the String who:
who = console.readLine("Who's there?  ");
  isBanana = (who.equalsIgnoreCase("banana"));
}while(isBanana);
// Person B responds:
console.printf("%s who?\n", who);
console.printf("Orange you glad i didn't say Banana anymore? \n");
// ==== END PROMPTING CODE ====
      String who; 
      boolean isBanana; 
      boolean isOrange; 
      do { 
        console.printf("Knock Knock. \n"); 
        who = console.readLine("Who's there? \n"); 
        isBanana = (who.equalsIgnoreCase("banana")); 
        isOrange = (who.equalsIgnoreCase("orange"));
      }while(isBanana); 
        if (isOrange);{

          console.printf("%s who? \n", who); 
          console.printf("%s you glad i didn't say banana anymore? \n", who); 
        }
    }
}
//yup, tried it and it works this way too.

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi David,

Well let's think that through a bit. Is there ever a case where those would both be true? I think what you might be getting at is checking to make sure that they entered orange. One thing you can do is negate a boolean. This is done by putting an exclamation mark in front of it. So what this code does is loop until the user enters orange.

// ...
boolean isOrange;
do {
// ...
     isOrange = who.equalsIgnoreCase("orange");
} while(! isOrange);

Since we didn't cover negation in Java Basics, this is not expected to be needed. Also to try and keep things simple the instructions state:

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

Great job thinking outside the box!

Hey, thanks Craig. That negation thing is superb.