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!

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

I cant figure out what i am doing wrong on this question: define a string with first name value set to your name.

this was my answer; which is incorrect- console.printf ("hello, my name is firstname");

string FirstName= "Amanda";

// I have setup a java.io.Console object for you named console
console.printf ("hello, my name is firstname");
string FirstName = "Amanda";

2 Answers

Luciano Bruzzoni
Luciano Bruzzoni
15,518 Points

Hello, You are getting an error because of two reasons,

1) before you use a variable, like in this case firstName, you must declare it first. So your first line of code should be the string declaration and then the printf function.

2) after you fix that, you will get another error because you are using the variable firstName incorrectly. When using a string variable, you don't need to put quotes around them since you are doing that already when you declare the string. Instead you want to add it the message by using a plus sign, like this:

console.printf("hello, my name is " + firstName);

Hope that helped! good luck.

Rafael Valera
Rafael Valera
8,401 Points

First declare the variables that you want to use. Second the data type String is misspelled Third use the string placeholder %s inside the first parameter of the printf method, this will make outputting strings more readable: console.printf("hello, my name is %s ", firstName); You should have an argument for every place holder separeted by a coma Ex: printf("%s likes %s %s", name, verb, sport)"