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

Define a string variable named first Name that stores your name?

I don't understand may someone help me thanks.

Name.java
import java.io.Console;

public class Test {

    public static void main(String[] args) {
        Console console = System.console();
        // Welcome to the Introductions program!  Your code goes below here
        console.printf("Hello, my name is Darrell");
  }
}

11 Answers

Thank You that worked but now I'm stuck on this code console.printf("%-Darryl can code in Java!"%-) what I'm doing wrong :(

michaelcodes
michaelcodes
5,604 Points

So, building ontop of the first line of code that we added, for Part 2 of the coding challenge we would add:

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

Now part 3 of the challenge is where we make use of our variable. We can replace the actual word "Darrell" in our printf with a %s, which is a placeholder for our variable. This looks like the following:

String firstName = "Darrell";
console.printf("%s can code in Java!", firstName);

When the above code runs, it will replace the "%s" inside of the string with our firstName variable.

Hope this helps, I know it can be confusing at first but keep at it :)

michaelcodes
michaelcodes
5,604 Points

Hi there! So in these coding challenges they have actually setup the class for you entirely. You won't need to put the class or the main method (public static void main). They have also setup the console object.

So you would only need to enter:

console.printf("Hello my name is Darryl");

However, the first part of the challenge is asking you to declare a variable an assign your name to it!

String firstName = "Me";

take care and happy coding!

Hi, Thank You for trying too help me. I input that second code and it gave me a error.

michaelcodes
michaelcodes
5,604 Points

Which part of the coding challenge are you on?

For part 1 I tested entering only the following and nothing else and it passed for me

String firstName = "Me";

For the second and third part we will add the printf statements

Hey Thank You. I run %=format s=space I think? But when I run it I get this error java.util.MissingFormatArgumentException: Format specifier '%s' ()

console.printf("%s Darrell can code in Java!"); is my code

michaelcodes
michaelcodes
5,604 Points

Hello again! The "%s"=String Placeholder (s= String). It is basically telling the compiler to expect a variable of type String. If the variable was an integer type we would use %d to represent it instead.

Your code below is close:

console.printf("%s Darrell can code in Java!"); 

However we must also specify which String variable the %s is going to be replaced with. You can add this parameter as such:

console.printf("%s can code in Java!", firstName); 

We can also remove your name, because that is stored in the firstName variable.

Thank You. I passed to next step because of you!!!! and this video on youtube:https://www.youtube.com/watch?v=LUv9OBVlFc4&index=21&list=PLG9j36Oetz2UDMdPzawuc0Ry5cVvZTV7S

I'm going to keep moving and i hope you still can help me right on this THREAD

michaelcodes
michaelcodes
5,604 Points

Awesome :) I am glad that I could help! And if you have any other questions I would be more than happy to help

Hi Thank You.

What operator do we use to ensure that both of these conditions are met: boolean isRefreshed = true; boolean isReadyToGetStarted = true; boolean shouldContinue = isRefreshed isReadyToGetStarted;

michaelcodes
michaelcodes
5,604 Points

Hello again, in order to ensure that both conditions are true we use a logical AND which is "&&" such as

boolean shouldContinue = isRefreshed && isReadyToGetStarted;

When I put it in I get a error: I was looking what && means true I forget what false it thank you. Please help

michaelcodes
michaelcodes
5,604 Points

Could you please post the link to the video/challenge that this is for so that I can better help

michaelcodes
michaelcodes
5,604 Points

The above question involving && is from the java objects course. This above link is to the java basics course. I am not sure which you are working on, but for the Java Objects question involving

boolean shouldContinue = isRefreshed && isReadyToGetStarted;

Since this is for a quiz, they would only want you to enter the && into the input box:

&&

so the above would be the answer

Thank You for trying to help me. I am stuck Define a string variable named firstName that stores your name. Set the value to your name. I been slacking for 6days so I'm here now and every other day because of how I learn no more slacking for me its 2018 I really need to complete this course. If you don't reply back today before 11pm 1130pmEST I will be back tommorow THANKS.

michaelcodes
michaelcodes
5,604 Points

Haha yes I understand the slacking definitely, I need to study more. So for the challenge it is broken down into 3 parts. They have setup everything else so going through the challenge part by part.

For part 1 it says: "Define a string variable named firstName that stores your name. Set the value to your name. "

To do so, in the challenge window we would type:

String firstName = "Darrell";

We have now stored the value "Darrell" in the first firstName variable. Everytime we call firstName, it will have Darrell

For part 2 it says: all the printf method on the console object and make it write out "<YOUR NAME> can code in Java!"

To do this, we want to add the following code under what we just created. (on line 3 if "String firstName = "Darrel" is on line 2)

console.printf("Darrell can code in Java!");

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

To do this, we want to Modify (or REPLACE) the statement we entered in part 2 like such:

console.printf("%s can code in Java!", firstName);

the "%s" is representing a string variable, which is specified as "firstName".

Hope you pass now!

I am going to try that out Thanks and get back with you

public static main why is it not that? Is it still a String Variables? public static main

michaelcodes
michaelcodes
5,604 Points

The reason we do not have public static main in here is specifically because of the treehouse challenge coding area. Treehouse has programmed it in a way that that stuff(public static main etc) is taken care of , so they don't require that you enter it.

The public static main method is part of what we call the "boiler plate". If you were making the file on your computer or using the workspaces at treehouse and compiling it (NOT in the code challenge), you would include:

public class Filename {
     public static void main(String[] args) {
     //Code goes here
    }
}

and you would have the file Filename.java.

There are videos that will explain what the public static void main method is and what it does etc. So don't worry about any of that right now!

Hey, I am going to skip this challange part its only #3 parts