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

Can someone show me this how it should look?

Now replace your name with the firstName variable using the string formatter.

Name.java
// I have setup a java.io.Console object for you named console
String firstName = "Rob";
console.printf ("%s can code in Java!");

6 Answers

Allie O.
Allie O.
11,601 Points

Make sure to add the firstName variable after your string:

console.printf ("%s can code in Java!", firstName);
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm not a Java programmer but I did this stage. In the stage, Craig printfs out a string to console where he put his name in. You need to copy that code but change the sentence that the name gets put into.

Remember, printf requires two things. The string to format and, ultimately, print, and the values to format the string with. You're only missing the value for formatting the string.

exactly Ken. And other students may understand it easier because most that do come on here have a basic knowledge of java or have used it. I have never touched it in my life. My games i code use a combination of basic python and php. believe it or not, it's different from what i am used to. so when a situation arises, im thinking of all the things i could do in python.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'm actually the Python teacher here at Treehouse. What Craig is asking you to do is exactly the same as this:

first_name = "Kenneth"
print("%s can code Python!" % first_name)

Or, more my style:

first_name = "Kenneth"
print("{} can code Python!".format(first_name))

Or, in PHP:

$firstName = "Kenneth";
echo sprintf("%s can code PHP!", $firstName);

Just, obviously, in Java.

Hopefully seeing how these are all very similar will help you as you go on!

i tried but i am failing on challenge task 3 of 3

yeah i just don't follow. This is supposed to be a learning curve and it's not, explanation format in the challenges just dont help at all. time to go to school i suppose there is no other way around it. for some people it's not so plain english. Thanks Ken for your time.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

OK, can you walk me through your mental process as you're reading and writing the code? Like, literally, line-by-line, step-by-step.

AWESOME ALLIE! THANKS! lol thats what i was looking for.

Directed to Ken: so when it says Now replace your name with the firstName variable using the string formatter.

This is how i Read it. //now replace your name "Rob" with the firstName variable //-->this is what i see it like: firstName "Rob"; <-- to me thats what firstName varibale means.

Next. //using the string formatter "%s". <--thats what i see. so in a complete format this is how i see it.

String firstName "%s"; lol . When the explanation is written that way i take it literally now, and that's what i come out with

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

So, assuming your code was:

String firstName = "Rob";
console.printf("Rob can code Java!")

at "Now replace your name with the firstName variable.", you see that as being in line 1, the line that starts with String, and not in line 2 in the string that's going to be printed?

Thanks, that'll hopefully help Craig and I make this more obvious to future students.

yeah i already have a funny feeling when seeing python. cause i can recall the codes better. I do it nearly everyday. First thin i learned in Python about things was print, str, expressions and etc. all that made sense and it was step by step so i picked it up naturally. when to use () or when to use [] and how to save things to memory and etc.. this maybe similar but writing it out is little different and honestly Videos are superb. but explanations is where i go bonkers. It's like studying for a test than next thing you know, the words are worded in a way where you have to dig deep into a part of your brain that understands them. for me it's not so easy cause im used to one method and when it switches. i feel like an idiot