Compilation error main java 8 error reached end of file while parsing

[Solved] Error: Reached end of file while parsing

«reached end of file while parsing» is a java compiler error. This error is mostly faced by java beginners. You can get rid of such kind of errors by coding simple java projects. Let’s dive deep into the topic by understanding the reason for the error first.

1. Reason For Error

This error occurs if a closing curly bracket i.e «>» is missing for a block of code (e.g method, class).

Читайте также:  Font family html шрифты

[Fixed] Reached end of file while parsing error in java

public class HelloWorld  public static void main(String args[])  System.out.println("This class is missing a closing curly bracket"); > 

If you try to compile the HelloWorld program using below command

Then you will get the reached end of file while parsing error.

reached end of file while parsing java error

If you look into the HelloWorld program then you will find there is closing curly bracket «>» missing at the end of the code.

Solution: just add the missing closing curly bracket at the end of the code as shown below.

public class HelloWorld  public static void main(String args[])  System.out.println("This class is missing a closing curly bracket"); > > 

2. More Examples

2.1 In the below example main() method is missing a closing curly bracket.

public class HelloWorld  public static void main(String args[])  System.out.println("Main method is missing a closing curly bracket"); > 

2.2 In the below example if block is missing a closing curly bracket.

public class HelloWorld  public static void main(String args[])  if( 2 > 0 )  System.out.println("if block is missing a closing curly bracket"); > > 

Similarly, we can produce the same error using while, do-while loops, for loops and switch statements.

I have shared 2.1 and 2.2 examples that you should fix by yourself in order to understand the reached end of file while parsing error in java.

3. How to Avoid This Error

You can avoid this error using the ALT + Shift + F command in Eclipse and Netbeans editor. This command autoformats your code then it will be easier for you to find the missing closing curly bracket in the code.

That’s all for today. If you have any questions then please let me know in the comments section.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry

Источник

Fix the Reach End of File While Parsing Error in Java

Fix the Reach End of File While Parsing Error in Java

  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

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.

public class MyClass   public static void main(String args[])    print_something();  > 
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();  >  > 

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");  > > 
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.

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.

public class MyClass   public static void main(String args[])   int x = 38;  while( x > 90)  // do something  System.out.println("Greater than 90");  x--;   > > 
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.

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");  > 
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");  > > 

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

Copyright © 2023. All right reserved

Источник

Reached end of file while parsing

The error Reached End of File While Parsing is a compiler error and almost always means that your curly parenthesis are not ending completely or maybe there could be extra parenthesis in the end.

Every opening braces < needs one closing braces >. The only purpose of the extra braces is to provide scope-limit . If you put curly braces in the wrong places or omit curly braces where the braces should be, your program probably won’t work at all. Moreover, If you don’t indent lines of code in an informative manner , your program will still work correctly, but neither you nor any other programmer will be able to figure out what you were thinking when you wrote the code.

How to avoid this error?

java netbeans eclipse ide

Because this error is both common and easily avoided , using a code editor like NetBeans or Eclipse . Using these IDE’s, you can autoformat your code by pressing Alt+Shift+F . This will indent your code properly and align matching braces with the control structure (loop, if, method, class) that they belong to. This will make it easier for you to see where you’re missing a matching brace .

Curly Braces in Java

In the context of a method or type ( class/interface/enum/annotation ), the < symbol is used to denote the beginning of the body of a class or a method :

It can also be used inside a class to declare an initializer or static initializer block:

reached < symbol

You can find that from above examples, each of these uses of the open brace symbol is different from all the others.

Источник

[Fixed] Reached end of file while parsing

In this post, we will see how to solve reached end of file while parsing in java.

We generally get this error when we forgot to end code of block(class/method) with > .

Let’s understand this with the help of example.

When you compile above java file, you will get below error.

Solution

To solve this issue, you just need to read code again and put curly brace at correct places.
For example:
In above program, we did not end main method correctly.

That’s all about how to solve reached end of file while parsing in java.

Was this post helpful?

Share this

Author

[Fixed] Unsupported class file major version 61 in Java

Table of ContentsReason for Unsupported class file major version 61 in JavaSolution for Unsupported class file major version 61 in JavaAndorid studio/Intellij Idea with gradleAny other scenario In this post, we will see how to fix Unsupported class file major version 61 in Java. Reason for Unsupported class file major version 61 in Java You […]

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

[Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

HashMap values cannot be cast to list

[Fixed] java.util.HashMap$Values cannot be cast to class java.util.List

Table of ContentsWhy HashMap values cannot be cast to list?Fix for java.util.HashMap$Values cannot be cast to class java.util.List In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List. Why HashMap values cannot be cast to list? HashMap values returns java.util.Collection and you can not cast Collection to List […]

Unable to obtain LocalDateTime from TemporalAccessor

[Fixed] Unable to obtain LocalDateTime from TemporalAccessor

Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

Источник

Оцените статью