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

Jocelyn Gan
Jocelyn Gan
1,481 Points

define a string variable

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

import java.io.Console; class FirstAttempt{ public static void main (String [] args){ Console console = System.console(); String firstName = "Jocelyn";

console.printf("My first name is %s\n",firstName);

} }

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

import java.io.Console;
class FirstAttempt{
  public static void main (String [] args){
    Console console = System.console();
    String firstName = "Jocelyn";

    console.printf("My first name is %s\n",firstName);

  }
}
Jocelyn Gan
Jocelyn Gan
1,481 Points

output: JavaTester.java:74: error: illegal start of expression import java.io.Console; ^ JavaTester.java:74: error: not a statement import java.io.Console; ^ 2 errors

4 Answers

Jocelyn Gan
Jocelyn Gan
1,481 Points

thx Adrian..actually the answer only:

String firstName = "Jocelyn";

console.printf("My first name is %s\n",firstName);

haha.. thx =]

Declare the class as public

Example:

public class FirstAttempt{ //your code here }

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Yeah, you don't need the class wrapper yet. Nice job overachieving though ;)

We'll get to it. Sorry for the confusion. I'll add a clear error for that.

Remove import java.io.console; this would fix because its already been hard coded as said in the comment

Jocelyn Gan
Jocelyn Gan
1,481 Points

Thx.. but public class Name{ public static void main (String [] args){ Console console = System.console(); String firstName = "Jocelyn";

console.printf("My first name is %s\n",firstName);

} }

still hav e error

Jocelyn Gan
Jocelyn Gan
1,481 Points

JavaTester.java:76: error: illegal start of expression public class Name{ ^ 1 error