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

Hassan Osman
Hassan Osman
628 Points

string firstname="hassan";console.printf("my name is hassan",firstname);

whats wrong

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

3 Answers

Ryan Ruscett
Ryan Ruscett
23,307 Points

String first name = "hassan"; < --- That's not right. This says create a String variable named first with .... ?

String firstName = "hassan"; <--- Says create a String variable with firtName as it's reference equal to Hassan

Perhaps you should watch the video again.

Ishay Frenkel
Ishay Frenkel
1,620 Points

Your code should look like this:

String firstName = "Hassan";
console.printf("My name is %s", firstName);

Okay, now the explanation:

First of all, make sure you type String with capital S, this is important.

variables should not contain spaces first name is wrong. Also best practice is to write with camel cases (thisIsAnExampleForTypingInCamelCase), FYI, you can't name variables with numbers in the beginning (2ndName is bad, but name2 is okay)

After a semicolon, move down a line, that's also a best practice and makes your code look better and more understandable.

if you type "my name is Hassan" you are not using the variable so use the variable, not the plain text of your name.

Ryan Ruscett
Ryan Ruscett
23,307 Points

The first thing I see is that you didn't capitalize the s in String. You must capitalize String for reasons to complicated to explain here.

Then your printf is a formatting print. So you need TWO arguments. So you need a string to print and a variable in which to use inside the string.

%s is a place holder within a string for some variable. For example

String variable = "cool";
console.printf("Something is %s", variable);

This returns

Something is cool.

You can put them on the same line but I wouldn't do that at this point.

Let me know if that clears it up for you!

Hassan Osman
Hassan Osman
628 Points

here is my code not working String first name = "hassan"; console.printf("my name is %s",firstname);