Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.

 

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)

 



	   
 
      

                         

» View all related tutorials
Related Tags: c exception io stack type exec uri lock this block exe row handle program to execution ram each pos ast

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.