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

why hit an error because class name and file name is same?

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

Name.java
import java.io.Console;
public class Name {
    public static void main(String[] args) {
        Console console = System.console();
        // Welcome to the Introductions program!  Your code goes below here
        String firstName = "Amir Al Hafiz";
        //thisIsAnExampleOfCamelCasting
        console.printf("Hello my name is %s \n", firstName);
        console.printf("%s is learning how to write Java \n", firstName);

  }
}

This is the exact code from a workspace.

Todd Anderson
Todd Anderson
4,260 Points

I think that Name is a special word in java. Try calling your class FirstName and see if that works.

https://docs.oracle.com/javase/8/docs/api/index.html?javax/xml/soap/Name.html

I changed but still hit error

// I have setup a java.io.Console object for you named console public class FirstName {

public static void main(String[] args) {
    Console console = System.console();
    // Welcome to the Introductions program!  Your code goes below here
    String firstName = "Amir Al Hafiz";
    //thisIsAnExampleOfCamelCasting
    console.printf("Hello my name is %s \n", firstName);
    console.printf("%s is learning how to write Java \n", firstName);

} }

------------------------------Error------------------------------ JavaTester.java:73: error: illegal start of expression public class FirstName { ^ 1 error ------------------------------Error------------------------------

1 Answer

Todd Anderson
Todd Anderson
4,260 Points

I copied and pasted what you had into the workspace and it worked fine? Does your class FirstName match the name of your java file? Should both be FirstName i.e FirstName.Java, class FirstName.