Home Java Java-exception Integer exception in java

Related Tutorials


 
 

Share on Google+Share on Google+

Integer exception in java

Advertisement
The integer class is a wrapper for integer value. Integers are not objects.

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[] argsthrows 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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 13, 2009

Related Tutorials

Discuss: Integer exception in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com