Search code examples
javacompiler-errorscompilation

Java Compile Time Error: reached end of file while parsing


public class Test{
    public static void main(String[] arg){
        System.out.println("Alexander The Great");
    }
}

In this code, if I remove the first quotation mark in the string, I get 3 errors. 1. ')' expected 2. unclosed string literal 3. not a statement

ok, I understood above errors.

If I remove the second quotation mark in the string, I also get 3 errors. 1. unclosed string literal 2. ';' expected 3. reached end of file while parsing

I understood 1 and 2, but why 3??

Doesn't it usually occur when braces are missing? But clearly, I have correct number of braces. Also, why not the first case give the error "reached end of file while parsing"?

P.S. I'm using jGrasp, java 1.6.


Solution

  • Leaving off the first quote potentially gives the parser a bit more to work with, because it will try to treat "Alexander" as a reference. Once it hits the space, the only legal stuff is the rest of an expression, but instead it thinks it's another identifier.

    It may be inserting a bonus ) to try to recover; depends on the implementation.

    Leaving off the last quote will send the parser to the end of the line. Instead of trying to close off that statement, it gets a little lost and spins off and can't recover and hits the end of the file.

    Off the top of my head, to me it seems that since Java strings are single-line only, it could do much the same thing and try to close off the string and/or statement in that case and recover a bit more gracefully. I might just be missing something obvious, though.