Fix the Reach End of File While Parsing Error in Java

Mohammad Irfan Jan 30, 2023 Dec 30, 2021
  1. reached end of the file while parsing - Missing Class Curly Brace in Java
  2. reached end of the file while parsing - Missing if Curly Block Brace in Java
  3. reached end of the file while parsing - Missing Loop Curly Brace in Java
  4. reached end of the file while parsing - Missing Method Curly Brace in Java
  5. Avoiding the reached end of file while parsing Error in Java
Fix the Reach End of File While Parsing Error in Java

This tutorial introduces an error reach end of the file while parsing during code compilation in Java.

The reached end of the file while parsing error is a compile-time error. When a curly brace is missing for a code block or an extra curly brace is in the code.

This tutorial will look at different examples of how this error occurs and how to resolve it. The reached end of file while parsing error is the compiler’s way of telling it has reached the end of the file but not finding its end.

In Java, every opening curly place ({) needs a closing brace (}). If we don’t put a curly brace where it is required, our code will not work properly, and we will get an error.

reached end of the file while parsing - Missing Class Curly Brace in Java

We missed adding closing curly braces for the class in the example below.

When we compile this code, it returns an error to the console. The reached end of file while parsing error occurs if the number of curly braces is less than the required amount.

Look at the code below:

public class MyClass {
    public static void main(String args[]) {
  
      print_something();
    } 

Output:

MyClass.java:6: error: reached end of file while parsing
    }
     ^
1 error

The closing brace of the MyClass is missing in the above code. We can solve this issue by adding one more curly brace at the end of the code.

Look at the modified code below:

public class MyClass {
   static void print_something(){
     System.out.println("hello world");
   }
    public static void main(String args[]) {
  
      print_something();
    }  
  } 

Output:

hello world

Let us look at the examples where this error can occur.

reached end of the file while parsing - Missing if Curly Block Brace in Java

The if block is missing the closing curly brace in the code below. This leads to the reached end of the file while parsing error during code compilation in Java.

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      if( x > 90){
        // do something
          System.out.println("Greater than 90");
    }  
}

Output:

MyClass.java:8: error: reached end of file while parsing
}
 ^
1 error

We can resolve this error by adding the curly brace at the appropriate place (at the end of the if block). Look at the code below:

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      if( x > 90){
        // do something
          System.out.println("Greater than 90");
      } // this brace was missing
    }  
}

The above code compiles without giving any error.

Output:

Greater than 90

reached end of the file while parsing - Missing Loop Curly Brace in Java

The missing curly braces can be from a while or a for loop. In the code below, the while loop block is missing the required closing curly brace, leading to a compilation failure.

See the example below.

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      while( x > 90){
        // do something
        System.out.println("Greater than 90");
        x--;
      
    }  
}

Output:

MyClass.java:10: error: reached end of file while parsing
}
 ^
1 error

We can resolve this error by putting the curly brace at the required position (at the end of the while loop). Look at the modified code below:

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      while( x > 90){
        // do something
        System.out.println("Greater than 90");
        x--;
      } // This brace was missing
    }  
}

The above code compiles without giving any error.

Output:

Greater than 90

reached end of the file while parsing - Missing Method Curly Brace in Java

In this case, we have defined a method whose closing brace is missing, and if we compile this code, we get a compiler error. Look at the code below.

public class MyClass {
    
    public static void main(String args[]) {
      customFunction();
    }  
    static void customFunction(){
      System.out.println("Inside the function");
    
}

Output:

MyClass.java:9: error: reached end of file while parsing
}
 ^
1 error

We can resolve this error by putting the curly brace at the required position (at the end of the function body). Look at the modified code below:

public class MyClass {
    
    public static void main(String args[]) {
      customFunction();
    }  
    static void customFunction(){
      System.out.println("Inside the function");
    }
}

Output:

Inside the function

Avoiding the reached end of file while parsing Error in Java

This error is very common and very easy to avoid.

To avoid this error, we should properly indent our code. This will enable us to locate the missing closing curly brace easily.

We can also use code editors to automatically format our code and match each opening brace with its closing brace. This will help us in finding where the closing brace is missing.

Related Article - Java Error