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 Getting Started with Java IO

Austin Cumberlander
Austin Cumberlander
13,997 Points

I think this challenge question is wrong.

This challenge wants me to use the printf() method using the given string "First name:" and attach it to the variable firstName, which has an assigned value to it. Can someone explain why my code is being rejected? Thanks!

IO.java
// I have imported java.io.Console for you.  It is a variable called console.
String firstName = "Austin";
console.readLine("firstName");
String lastName = "Cumberlander";
console.readLine("lastName");

console.printf("First name:%s\n", firstName);

4 Answers

Hi there,

You haven't used user input to set the values held in the variables.

To set the firstName you want a line like this:

String firstName = console.readLine("What is your first name?: ");

Correcting that will let your code pass. This allows the user to enter their first name, rather than having it hard-coded. I'm guessing the tests behind the challenge are setting that user input but not seeing it stored in the firstName variable as it expects. You'll need to do the same with lastName too.

I hope that helps,

Steve.

Austin Cumberlander
Austin Cumberlander
13,997 Points

Not to come off as curt, but I think this challenge description is poorly written. I still don't understand what it's asking. What's the solution in this case? I tried to use your suggested statement with the the additional printf() method but now it says, "Oops! It looks like Task 2 is no longer passing." This is what I wrote:

String firstName = console.readLine("What is your first name?: ");
  console.printf("%s", firstName);

Where did the completely new string come from? ("What is your first name?:") On the challenge it only says "First name:".

Hi Austin,

The challenge is split into various stages, as you know. The first is, Declare a variable that is named the camel-cased version of "first name". Store the user's first name into this new variable using console.readLine

So, this means we're asking the user for input - that's what console.readLine() does - it prompt the user for input. The string inside the brackets is the prompt which is where the string "What is your first name" came from. We're asking the user to enter their name. You can put any string, or nothing, in there, though. There's two versions of this method - one takes a prompt string, the other doesn't. The docs detail these two methods.

So, in essence, the challenge wants you to ask the user for their first name and then their last name, and then use a formatted string to output them both. The output is using printf and string interpolation using the %s placeholder within a defined template such as "First name: ".

Your second code snippet hasn't worked because you didn't put the string output in your printf - the challenge is expecting the output First name: Austin.

In all the challenge would look like:

String firstName = console.readLine("What is your first name?: "); 
String lastName = console.readLine("What is your last name?: "); // both the prompt strings are optional but clearer
console.printf("First name: %s", firstName);
console.printf("Last name: %s", lastName);

I hope that makes sense.

Steve.

Austin Cumberlander
Austin Cumberlander
13,997 Points

I appreciate your explanation. I have a better understanding of what's being asked. Thank you very much!

However, I do want to point out that #3 of this challenge only asks for the first name to be printed as opposed to both. So when I input the code so that it only shows the first name as asked, it gives an error.

String firstName = console.readLine("What is your first name?: ");

console.printf("First name: %s", firstName);

The code you gave does pass (thanks again!), but I would have never known otherwise that the challenge wanted both the first and last name prompts.

I'm sorry for pestering. I'm just trying to give constructive feedback. I'm not trying to upset you.

Hi Austin,

I'm not getting upset - I'm just trying to make sure I've helped you out so don't worry about that!

The 4th task requires the last name to be printed out - my code was the complete challenge's final code. There are 4 tasks, The first asks for the user input for first name, second asks for last name to be input, third outputs the first name and the last, fourth, task outputs the last name.

What error are you getting?

I think the challenge builds from the what the video teaches. So, at 1m15s in the video, where Craig uses readLine() to prompt the user for their name; the challenge repeats this point to try to reinforce the learning.

Steve.

Austin Cumberlander
Austin Cumberlander
13,997 Points

You have helped for sure! And I know that the fourth question asks for the last name, but what I'm saying is that on number #3, it only asks for the first name.

Challenge 3 of 4 "Using the console's printf method, display a message that says, "First name: ", followed by the first name that the user has entered."

And when I use the following code:

String firstName = console.readLine("What is your first name? ");
console.printf("First name: %s", firstName);

That's when it gives the error "Oops! It looks like Task 2 is no longer passing."

When I use the code you've provided, which includes the last name prompt, it works for #3. When I use the exact same code for #4, it works here as well.

That will be because task 2 requires the line:

String lastName = console.readLine("What is your last name?: ");

So if that's missing when you try task 3, it'll say task 2 isn't passing as the variable lastName won't hold a value.

As long as you've completed the challenge and understood its content, we're all good. :+1: