Home Java Java-exception Number Format Exception
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Number Format Exception

Advertisement
NumberFormatException is a subclass of the Runtime Exception class. A Number Format Exception occurs in the java code when a programmer tries to convert a String into a number.

Number Format Exception

     

NumberFormatException is a subclass of the Runtime Exception class.  A Number Format Exception occurs in the java code when a programmer tries to convert a String into a number. The Number might be int,float or any java numeric values. 

Understand Number Format Exception

The conversions are done by the functions Integer.parseInt and Integer.parseDouble.  Consider the function call Integer.parseInt(str) where str is a variable of type String. Suppose the value of str is  "60", then the function call  and convert the string into the int 60. However, if you give the value of str is "saurabh", the function call will fail to compile because "saurabh" is not a legal string representation of an int value. In such case,  NumberFormatException will occurs

 

 

 

public class ConvertStringToNumber
{

   public static void main(String[] args)
{


   try


{


   String s = "saurabh";


   int i = Integer.parseInt(s);


  // this line of code will never be reached//


   System.out.println("int value = " + i);

}


   catch (NumberFormatException nfe)


{
 

  nfe.printStackTrace();

}


}

}


Output on Command Prompt


C:\Documents and Settings\Administrator>cd\

C:\>cd saurabh\

C:\saurabh>javac ConvertStringToNumber.java

C:\saurabh>java  ConvertStringToNumber
java.lang.NumberFormatException: For input string: "saurabh"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at ConvertStringToNumber.main(ConvertStringToNumber.java:9)

 



	  
 
      




				
					






					
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: October 22, 2008

Related Tutorials

Ask Questions?    Discuss: Number Format Exception   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
shobin
December 9, 2011
good

PreparedStatement pstmt= null; pstmt1=conn.prepareStatement("Select loanreq_seq.nextval from dual"); rs = pstmt1.executeQuery();
sridhar
January 15, 2013
NumberFormatException

I am getting NumberFormatException for input String="" so how do i handle this type of exception??
Pat
April 6, 2012
NumberFormatException

Pls how can I resolve such problem of passing a string to a value. Thank you
Sunil Chaurasia
June 7, 2012
Core Java and Advanced Java

it is very good example
DMCA.com