Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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";

Name.java
// 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)"