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

Srikanth Nallagundla
PLUS
Srikanth Nallagundla
Courses Plus Student 231 Points

Hi , I am unable to find error in the below code.

public class Name { public static void main(String[] args) { Console console = System.console(); String First = "Sri"; console.printf("HI, my name %s", First);
} } Kindly resolve this

Name.java
// I have setup a java.io.Console object for you named console
public class Name 
{
  public static void main(String[] args)
  {
    Console console = System.console();
    String First = "Sri";
    console.printf("HI, my name %s", First);  
  }
}

6 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello it still looks like you are overthinking the problem, there's no need to define a class or anything like that and create the method, you'll be working on that later in the course, all that needs to be done in this challenge is to set the string firstName which you have correctly done and print it out correctly,

String firstName = "Sri"; 
console.printf("%s can code in java", firstName ); 

You've correctly done the two parts that need to be done in this challenge, if you erase eveyrthing before and after the section that I have listed above you will pass it. It looks like something is getting caught up in the class declaration and the console importing.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello, you're close on this. One thing you might want to do is take out your console declaration, as the commented out section states one has imported so we don't need it.

Other than that the only thing you need to do is they are expecting the printed out statement to be "<your name> can code in java"

So it should look something like.

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

Let me know if this helps, looks like it was just something as simple as printing out the wrong output than what the challenge was coded to accept.

Srikanth Nallagundla
PLUS
Srikanth Nallagundla
Courses Plus Student 231 Points

Following is the error am getting . help solving or can pls rewrite the code and send me .so that i can understand better

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

public class Name { public static void main(String[] args) { // Console console = System.console(); String First = "Sri"; console.printf("HI, my name %s", First); } }

Error:

JavaTester.java:53: error: illegal start of expression import java.io.Console; ^ JavaTester.java:53: error: not a statement import java.io.Console; ^ JavaTester.java:55: error: illegal start of expression public class Name ^ 3 errors

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello, these are the lines of code that need to be typed to complete the challenge, since console is already defined there's no need to import another.

String firstName = "Sri";
console.printf("%s can code in java", firstName);

you want to be sure to call the variable firstName to, it seems nit-picky but best to give the challenge exactly what it asked for.

You're then printing out that line formatted with firstName inserted in.

Srikanth Nallagundla
PLUS
Srikanth Nallagundla
Courses Plus Student 231 Points

we have tried as what you have defined. Please find the below piece of code. public class Name { public static void main(String[] args) { // Console console = System.console(); String firstName = "Sri"; console.printf("%s can code in java", firstName ); } }

Again it shows the error, JavaTester.java:53: error: illegal start of expression public class Name ^ 1 error

Jonathan Söder
Jonathan Söder
7,428 Points

Hi Srikanth!

Just like Rob says the only lines of code that need to be typed in is the one's he mentions above. The challenges can be very nitpicky. But what you need to do is basically this:

//Remove these three lines completely:
public class Name { 
public static void main(String[] args) { 
Console console = System.console(); 



// The challenge is only looking for the the two lines below. 
// Anything else might result in an error like the one you encountered.
String firstName = "Sri";
console.printf("%s can code in java", firstName );