Integer exception in java

The integer class is a wrapper for integer value. Integers
are not objects. The Java utility classes require the use of objects. If you
want to store an integer in a hash table, you have to "wrap" an Integer
instance in it.
Understand
with Example
The Tutorial illustrate a code that helps you in
understanding a Integer exception in java. The program given below describes the way of handling
integer exception .In the program given below we have created -
1) Buffer Reader - This is used to read text from the character input stream. The program will ask
the user to enter the number , if the user enter the number other than integer
it will gives an exception error. if the user enters integer it will display the
number which the user have entered.
2) Read line( ) - This is used to read a line
from the character
3)Integer.toString( ) - This return you the string
representation irrespective of any data type passed as argument in it.
In case the user enter a value other than integer, exception
will caught in the catch block and println print the "Please enter the
value as integer".
IntegerException.java
import java.io.*;
public class IntegerException {
public static void main(String[] args) throws IOException {
try {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the integer value:");
String s = buff.readLine();
String doub = new Double(s).toString();
System.out.println("Double:=" + doub);
} catch (Exception ex) {
System.out.println("Please enter the value as integer");
}
}
}
|
|
Output of the program
Enter the integer value:
g
Please enter the value as integer |
Download source code

|