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)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ak17
  • 75
  • 3
  • 15
  • 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? – Hovercraft Full Of Eels Oct 18 '13 at 03:53
  • 1
    Post the `show()` and `display()` methods. Errors seems to be in them, apart from the `New` keyword, which should have been `new`. – Rahul Oct 18 '13 at 03:54
  • 1
    @HovercraftFullOfEels: The class doesn't *have* to be declared public. – Makoto Oct 18 '13 at 03: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? – Makoto Oct 18 '13 at 03:56
  • This is my Sample.java :class Sample { public void display() { System.out.println("Hello"); } public void show() { System.out.println("Good Day"); } } – Ak17 Oct 18 '13 at 04:07
  • I am still facing this error :Sam.java:6: ';' expected Sample p = New Sample(); ^ – Ak17 Oct 18 '13 at 04:11

4 Answers4

4

The new keyword has lower case characters :

Sample p = new Sample();
Crickcoder
  • 2,135
  • 4
  • 22
  • 36
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() {}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    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)! :) – Rahul Oct 18 '13 at 03:57
  • @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. – Andrew Thompson Oct 18 '13 at 04:04
  • 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 – Rahul Oct 18 '13 at 04:07
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

Community
  • 1
  • 1
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
2

-

remember one point :

all java key words are in lower case characters.

-

Eddy
  • 3,623
  • 37
  • 44