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

Jeevana Jyothi Bommannagari
Jeevana Jyothi Bommannagari
3,457 Points

Define a string variable named firstName that stores your name. Set the value to your name.

Define a string variabl e named firstName that stores your name. Set the value to your name.

Name.java
// I have setup a java.io.Console object for you named console
public class JavaDemo{
  public static void main(String[] args){
Console obj =new Console();
String firstName="shobha";
System.out.println("Hello this is ", firstName);

  }
}

2 Answers

michaelcodes
michaelcodes
5,604 Points

Hi there! so in this code challenge area you actually do not need to put in all of the "boiler plate" portions of this code. This would be the public static void main line and setting up the console object. So for this it is only requiring:

String firstName="shobha";
System.out.println("Hello this is ", firstName);

But there is an issue here, you cannot use println with formatted strings, you need printf. And we can use %s to represent the string as such:

System.out.printf("Hello this is %s", firstName);

For the purposes of this code challenge they have already setup a console object, so to reduce the code a little we can instead use:

console.printf("Hello this is %s", firstName);

Mind you in effect this is the same thing as System.out.printf, just a little cleaner looking!

Happy coding!

A Y
A Y
1,761 Points

Hello Jeevana,

You have two bugs here in your code:

1) Declaring your Console object. Console obj = new Console(); //supposed to be System.console();

2) Printing your first name. System.out.println("Hello this is ", firstName); //supposed to be "System.out.printf("Hello this is ", firstName);"

Please modify your code by fixing these two bugs and it will work, try it: public class JavaDemo { public static void main(String[] args) { Console obj = System.console(); String firstName="shobha"; System.out.printf("Hello this is ", firstName); } }

Also, this will help you: https://teamtreehouse.com/community/difference-between-systemoutprintf-and-systemoutprintln

Regards, Amir