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

Brandon Wash
PLUS
Brandon Wash
Courses Plus Student 1,186 Points

how would this code be written correctly

How do i write this code correctly and what is the %s meaning in this code?

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?
        ...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);
Marcin Smałz
Marcin Smałz
13,284 Points

You have to write while loop with Orange as a exit condition. %s in this particular case means that it will be a string which is given as a parameter console.printf("%s who?\n", who); so it wille be replaced by who variable.

I will not write you a working code you shold think it now by yorself: while loop while (condition) {

} - condition have to be true:)

Good luck.

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Brandon;

If you are still uncertain what the %s does in this code, I would highly recommend re-watching the videos, especially the Strings and Variables module as Mr. Dennis covers the explanation and use of the string formatter, %s there. Much of this course, the Java Objects course, and later Java development somewhat depends on a solid understanding this type of syntax, or at least the thought process behind it. Without a good grasp on it in the beginning the Java learning curve will be steeper.

Similarly, Looping is another important aspect of most programming languages and reviewing that video would be recommended as well.

In terms of your direct question, try it out and post your code here on the forum if you can't get it to work. I'm certain someone will provide some helpful insights.

Happy coding and welcome to Treehouse.

Ken