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

SHANTANU RANA
PLUS
SHANTANU RANA
Courses Plus Student 524 Points

what would be the answer for this ? As I am stuck doing the lab work and would seek an answer different to what m gettin

define a string and assign it a value

Name.java
// I have setup a java.io.Console object for you named console

    String firstName = Shaun;

    console.printf("My name is %s Rana");
  }
}
dusanveljkovic
dusanveljkovic
1,035 Points

String firstName = Shaun;

console.printf("My name is %s Rana", firstName);

} }

%s stand for string which is firstName in your code you are missing a ( , ) and a variable ( firstName)

1 Answer

While Dusan answered your question partially you have no defined your string correctly. All strings must be enclosed in quotes. What you are trying to do is set the string equal to another variable.

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

Notice how "My name is %s Rana." Is enclosed in quotes? Because it's a string, and variables are no different. Also you must pass the printf method which string you would like it to use for your %s in the string, you do this by separating with a comma and typing the string name. Hope that helps!

Happy coding!