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 trialFrank Tocci
2,316 PointsWon't recognize %s in Java when using console.printf
It's giving me the message, "Did you forget to pass the firstName
parameter to the printf function?" when I try to run my code. I can't figure out what's wrong with it.
// I have imported java.io.Console for you. It is a variable called console.
String firstName = "";
console.readLine("First name?", firstName);
String lastName = "";
console.readLine("Last name?", lastName);
console.printf("First name: %s", firstName);
2 Answers
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHey Frank.
What Mark is trying to say is that you're never assigning the value of your of the input from console.readLine() into the variable, you intialize it as blank. There's no reason to do that yet unless you wanted to continue to ask while I value was not null.
To properly assign a value with the readLine fuction you'd use something like below
String myVariable = console.readLine("What is your variable?");
Then I could use the assignment to do something below like the challenge is requesting by saying
console.printf("Your variable is %s", myVariable);
You are running into trouble because you are trying to pass in the variable value in the same line where you try to find it. Your readLine() command does not need to have the value passed into it. This will cause the error that you are seeing.
Thanks, let me know if this doesn't help.
mark gilchrist
Courses Plus Student 33,169 PointsThat is because you are not assigining a value to firstname change String firstName = ""; console.readLine("First name?", firstName); to String firstName = console.readLine("First name?", firstName);
Frank Tocci
2,316 PointsString firstName = console.readLine("First name?", firstName); String lastName = ""; console.readLine("Last Name?", lastName); console.printf("First name: %s",firstName);
Doesn't work: Bummer! There is a compiler error. Please click on preview to view your syntax errors!
Frank Tocci
2,316 PointsFrank Tocci
2,316 PointsThanks, it works now