The Java error illegal start of type occurred in your code when the starting braces of catch block is not used after the close of try block.
In this tutorial we want to describe a code that helps you in understanding the coedeof Illegal start type, for this we have a class name Illegalstartoftype.Inside this main method we have a try block that
include -1)InputStreamReader-An InputStreamReader is the way to convert from byte stream into character stream.
2)Buffered Reader- This used to read a text from a input stream to provide you efficient mean of reading character.
3)System.out.println-This is used to display the result on the command prompt
we have taken a variable string that is used to store the number entered on your command prompt and read the number from it by calling a readline method( ) . The for loop is used to check the condition if the value of i<j ,the system.out.print display the value of i in the try block. Otherwise the try block is caught by subsequent catch block to handle the error in the try block.
Illegalstartoftype.java
|
import java.io.*; public class Illegalstartoftype { public static void main(String args[]) throws Exception { try{ InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inputStreamReader); System.out.println("Enter a number : "); String num = br.readLine(); int j = Integer.valueOf(num); for (int i = 0; i < j; i++) { System.out.println(i); }} }catch(Exception ex){ System.out.println(ex); } } |
|
Compiling 1 source file to /home/girish/NetBeansProjects/errors/build/classes /home/girish/NetBeansProjects/errors/src/ Illegalstartoftype.java:7: 'try' without 'catch' or 'finally'
try{
|
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java error illegal start of type View All Comments
Post your Comment