Solved : Reached End of File while parsing error in Java

This error generally comes when you are new to Java or too tired of working and then too developing something in java class.. LOL.. 🙂

In java if you try to execute following code, it will give you error.






import java.io.*;
import java.lang.*;
public class DemoClass

public static void main(String args[])
{
try
{
InputStreamReader read =new InputStreamReader (System.in);
BufferedReader in =new BufferedReader (read);
System.out.println(" Select the option");
System.out.println(" 1. International");
System.out.println(" 2. National");
System.out.println("");
System.out.println(" Enter your option");
int option=0;
try
{
option = Integer.parseInt(in.readLine());
}
catch(Exception e)
{
option=-1;
}
if(option==-1)
{
System.out.println("Invalid Input");
}
if(option==1)
{
// do something
}
if(option==2)
{
// do something
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

It is because The compiler was parsing your code and was expecting another token, likely an ending brace: ‘{}’ to end the class but instead reached the end of the stream.

In layman’s language, you are missing a brace to close your class definition.

Here we haven’t enclosed the class with {} so it is giving error. it should be as follows.







import java.io.*;
import java.lang.*;
public class DemoClass
{
public static void main(String args[])
{
try
{
InputStreamReader read =new InputStreamReader (System.in);
BufferedReader in =new BufferedReader (read);
System.out.println(" Select the option");
System.out.println(" 1. International");
System.out.println(" 2. National");
System.out.println("");
System.out.println(" Enter your option");
int option=0;
try
{
option = Integer.parseInt(in.readLine());
}
catch(Exception e)
{
option=-1;
}
if(option==-1)
{
System.out.println("Invalid Input");
}
if(option==1)
{
// do something
}
if(option==2)
{
// do something
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

and if you have already started your class with ‘{‘ then just format your code and put region on every function and check you must be missing closing braces ‘}’ for class.

Leave a Reply

Your email address will not be published.