1

I am a beginner in Java.I have JDK1.7.0 installed on windows 7 OS.I just wrote a sample java file where the file was not getting compiled and throws the below error.

Sam.java:5: ';' expected
                Sample p = New Sample();
                                            An exception has occurred in the compile
        r (1.7.0-ea). Please file a bug at the Java Developer Connection (http://java.su
        n.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include
        your program and the following diagnostic in your report.  Thank you.
        java.lang.StringIndexOutOfBoundsException: String index out of range: 26


     at java.lang.String.charAt(String.java:694)
        at com.sun.tools.javac.util.Log.printErrLine(Log.java:251)
        at com.sun.tools.javac.util.Log.writeDiagnostic(Log.java:343)
        at com.sun.tools.javac.util.Log.report(Log.java:315)
        at com.sun.tools.javac.util.AbstractLog.error(AbstractLog.java:96)
        at com.sun.tools.javac.parser.Parser.reportSyntaxError(Parser.java:295)
        at com.sun.tools.javac.parser.Parser.accept(Parser.java:326)
        at com.sun.tools.javac.parser.Parser.blockStatements(Parser.java:1599)
        at com.sun.tools.javac.parser.Parser.block(Parser.java:1500)
        at com.sun.tools.javac.parser.Parser.block(Parser.java:1514)
        at com.sun.tools.javac.parser.Parser.methodDeclaratorRest(Parser.java:25
69)
        at com.sun.tools.javac.parser.Parser.classOrInterfaceBodyDeclaration(Par
ser.java:2518)
        at com.sun.tools.javac.parser.Parser.classOrInterfaceBody(Parser.java:24
45)
        at com.sun.tools.javac.parser.Parser.classDeclaration(Parser.java:2290)
        at com.sun.tools.javac.parser.Parser.classOrInterfaceOrEnumDeclaration(P
arser.java:2228)
        at com.sun.tools.javac.parser.Parser.typeDeclaration(Parser.java:2217)
        at com.sun.tools.javac.parser.Parser.compilationUnit(Parser.java:2163)
        at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:530)
        at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:571)
        at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:82
2)
        at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:748)
        at com.sun.tools.javac.main.Main.compile(Main.java:386)
        at com.sun.tools.javac.main.Main.compile(Main.java:312)
        at com.sun.tools.javac.main.Main.compile(Main.java:303)
        at com.sun.tools.javac.Main.compile(Main.java:82)

        at com.sun.tools.javac.Main.main(Main.java:67)

Below is the code for Sam.java file

class sam
{
    public static void main(String args[])

    {
    Sample p = New Sample();

    p.show();
    p.display();


    }
}

I researched in google with the various compiler options but that did not help.I would like to understand the below errors. 1 - Sam.java:5: ';' expected 2 - An exception has occurred in the compiler (1.7.0-ea)

6
  • 2
    The class should be Sam not sam if the file is named Sam.java. Class names should begin with an upper case letter. Also the class should be public. Also, is there in fact another class called Sample? Commented Oct 18, 2013 at 3:53
  • 1
    Post the show() and display() methods. Errors seems to be in them, apart from the New keyword, which should have been new. Commented Oct 18, 2013 at 3:54
  • 1
    @HovercraftFullOfEels: The class doesn't have to be declared public. Commented Oct 18, 2013 at 3:55
  • 1
    It seems that you're using an early release version of the Java compiler. Have you tried updating to the most recent version? Commented Oct 18, 2013 at 3:56
  • This is my Sample.java :class Sample { public void display() { System.out.println("Hello"); } public void show() { System.out.println("Good Day"); } } Commented Oct 18, 2013 at 4:07

4 Answers 4

4
Sample p = New Sample();

Should be:

Sample p = new Sample();

Java is case sensitive.


E.G. This compiles cleanly here:

class Sam // class names should be EachWordUpperCase
{
    public static void main(String args[])
    {
    Sample p = new Sample();

    p.show();
    p.display();
    }
}

class Sample {
    public void show() {}
    public void display() {}
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think there's more to it, than just this new keyword problem. You better hang in around here to edit your answer with those as well(if the OP posts those 2 erroneous methods throwing the StringIndexOutOfBoundsException error)! :)
@R.J (shrugs) Can't say I noticed the StringIndexOutOfBoundsException when I first commented, but the OP should not be attempting to run code that does not compile cleanly. My advice would be to fix the compiler errors first, then ask about other matters in other questions.
I would expect the same, but unfortunately, the OP has no such plans. :( A few more mins, and I move on from this question, like FOREVER. :D
4

The new keyword has lower case characters :

Sample p = new Sample();

Comments

3

make Sample p = New Sample(); to Sample p = new Sample();

new is a reserved keyword which is used in java to create an object but as java is case sensitive new is different from New

There are also different ways of creating objects click to know more

Comments

2

-

remember one point :

all java key words are in lower case characters.

-

2 Comments

You're entirely correct, but your answer doesn't add much to the existing corps of upvoted answers...
you need expand your answer but as you are new to Stackoverflow so +1 from my side

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.