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 trialdaniel zaragoza
1,843 PointsI keep getting "Did you forget to pass the `firstName` parameter to the printf function?" What am i doing wrong??
Any help would be useful.
// I have imported java.io.Console for you. It is a variable called console.
String firstName = "first name";
String lastName = "last name ";
console.readLine( firstName);
console.readLine( lastName);
console.printf("%s First name: \n",firstName);
babbiches
306 PointsThis is what I did and it worked. Even though the challenge takes many different versions of this, I think this is the one that would be most right. Mostly because it allows the user to input their name which I think was the purpose of the challenge.
String firstName = console.readLine("What is your first name?");
String lastName = console.readLine("What is your last name?");
console.printf("First Name: %s \n", firstName);
console.printf("Last Name: %s \n", lastName);
so it looks like you forgot the console.readLine, and alsoif you kept firstName = "first name" and you ran it it would come out as First name: first name. I hope this helps a little! Im new to this all together haha
1 Answer
Steve Hunter
57,712 PointsHi Daniel,
I agree with both comments above. You've called the readLine()
method, but not assigned the answer into the variable. The readLine
method is asking for user input, like:
String myDogsName = console.readLine("What's your dog's name?: ");
The user answers the question and his input is stored in the variable, myDogsName
. Where you've used the readLine()
method, you passed in firstName
as a parameter. You had previously set firstName
to "first name", so the user would be presented with a request for "first name", which is cool. But his answer would be lost as your line doesn't assign his answer to anything:
console.readLine(firstName); // answer goes nowhere
You need to assign the output of the method:
String firstName = console.readLine("What's your name?: ");
One thing; make sure the output is neat. There's a couple of variants in the suggestions, one is:
console.printf("%s First name: \n",firstName);
This outputs the variable content first, then the static text so would look like this (if I ran the code):
Steve First Name:
That's not quite where we're at. The second suggestion is better:
console.printf("First Name: %s \n", firstName);
As the output from that would be what we're looking for:
First Name: Steve
My solution, which is just one of many possible bits of working code looked like:
String firstName = console.readLine("What is your first name?: ");
String lastName = console.readLine("What is your last name?: ");
console.printf("First name: %s%n", firstName);
console.printf("Last name: %s%n", lastName);
The only real difference there is the use of %n
rather than \n
. They do the same thing, but %n
is a platform specific way of adding a new line. It is preferred over \n
but we're talking detail here; don't get hung up on stuff like that!
Hope that helped!
Steve.
Paul Hubbard
5,818 PointsPaul Hubbard
5,818 PointsHey! I just did the challenge, and I completed it. The problem is that you need to declare the console.readLine inside the variables for firstName and lastName. I've posted the full solution below. Hope this helps! :)
String firstName = console.readLine();
String lastName = console.readLine();
console.printf("%s First name: \n",firstName);
console.printf("%s Last name: \n",lastName);