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? Last coding exercise so confused!

please help i have no clue! The last one in this code exersicse: http://teamtreehouse.com/library/java-basics/perfecting-the-prototype/looping-until-the-value-passes

3 Answers

For the first stage you can try the code below.

String who;
// Person A asks:
do { 
    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?  ");
     console.printf("%s who?\n", who);
} while(who.equalsIgnoreCase("banana"));

After passing the first stage then for the second stage try the code below. if it helps please click best answer and if it doesnt ....give me a shout

if (who.equalsIgnoreCase("orange")){
console.printf(" %s you glad i did not ask for 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,

What this challenge is wanting you to wrap the entire joke in a do while loop.

The syntax is like below.

do {

// run through this code until the while condition is not met. 

} while (//condition that breaks loop);

So basically the first task is wanting you to put the whole condition in a loop and have it continue to go through the joke until who is not banana

We can do this by the follow

String who;
do {
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?  ");
} while (who.equalsIgnoreCase("banana"));
// Person B responds:
console.printf("%s who?\n", who);

Notice we declare who outside of the loop, this is because during the second task of this assignment, we will need to use who again, and if it was only defined in the loop, we wouldn't have the scope to access it.

Thanks, let me know if this doesn't help.

thanks rob! can you help with the second exercise! if so than your AWESOME!

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

Hey Kyle, I see you already got the answer, I just wanted to add a warning. The above code is having you change the already pre-written code and look through another if statement, that's not nessecary.

The only code that was needed to pass the second question is

console.printf("%s you glad I didn't say banana?", who);

Causing the program to go through two if statements isn't necessary in this challenge.

Thanks, glad we got it worked out for you.

Thanks everybody! you guys rock!