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 Strings, Variables, and Formatting

Now replace <YOUR NAME> in the console.printf expression with the firstName variable using the string formatter.

help

Name.java
// I have setup a java.io.Console object for you named console
String firstName = ("What is your name?"  );
console.printf ("<%s> can code in Java!", firstName  );
Mark Miller
Mark Miller
45,831 Points

The angle brackets must be removed from the string formatter. Just put %s with nothing around it.

Here is what i have Challenge Task 1 of 3 : SUCCESS Define a string variable named firstName that stores your name. Set the value to your name. Answer : String firstName= "John";

Challenge Task 2 of 3: SUCCESS Call the printf method on the console object and make it write out "<YOUR NAME> can code in Java!" Answer: String firstName= "Anika"; console.printf ("%s/n can code in Java!", firstName);

Challenge Task 3 of 3 : FAILED Now replace <YOUR NAME> in the console.printf expression with the firstName variable using the string formatter. My input: String firstName= "John"; console.printf ("%s/n can code in Java!", firstName); console.printf (firstName, "can code in Java!"); -----> this is the line that needs correction.

2 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Here is one way to do this:

import java.io.Console;

public class PrintYourName {

    public static void main(String[] args) {

        Console console = System.console();

        // Your code goes below here

        String firstName = "Sam";

        console.printf("Hello, my name is %s\n", firstName);

  }

}
Mark Miller
Mark Miller
45,831 Points

When you failed challenge 3, you overwrote the right answer with a wrong answer. Your answer to challenge 2 is actually the answer to challenge 3. For challenge 2, you should hard code your name, literally type the characters of your name in the print function. Then for challenge 3, it says replace the hard-coded characters now with the string formatter %s in place of your name, and put the variable name firstName after the comma after the quote. So use your challenge 2 answer (the one that passed) for the challenge 3 answer.

You can change your challenge 2 answer to have your name typed instead of the %s. There is more than one right answer for challenge 2 in this case because the programs are not necessarily specified to accept only one answer.