Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to handle NumberFormatException ?

  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Posts
    27
    Rep Power
    0

    Default How to handle NumberFormatException ?

    In this tutorial , we will discuss about NumberFormatException with example.NumberFormatClass is a subclass of the Runtime Exception class.It 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. The conversions are done by the functions Integer.parseInt and Integer.parseDouble.

    Java Code: NumberFormatException Example
    public class ConvertStringToNumber {
    
        public static void main(String[] args) {
            try {
                String s = "exception";
    
                int i = Integer.parseInt(s);
    
                // this line of code will never be reached//
    
                System.out.println("int value = " + i);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }
    
        }
    }
    Explanation of Example code:
    Consider the function call Integer.parseInt(str) where str is a variable of type String. Suppose the value of str is "100", then the function call and convert the string into the int 100. However, if you give the value of str is "exception", the function call will fail to compile because "exception" is not a legal string representation of an int value. In such case, NumberFormatException will occurs

    Output on Command Prompt

    How to handle NumberFormatException ?-number-format.jpg
    Figure:Output of the program

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to handle NumberFormatException ?

    Quote Originally Posted by Java Example View Post
    However, if you give the value of str is "exception", the function call will fail to compile because "exception" is not a legal string representation of an int value.
    The compiler couldn't and wouldn't care less; please stop spreading incorrect information (I'll inform the forum owner and ask him if he'd remove all your crap).

    Jos
    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. NumberFormatException
    By BurntCandy in forum New To Java
    Replies: 5
    Last Post: 10-09-2012, 03:28 AM
  2. NumberFormatException
    By laxtrappa in forum New To Java
    Replies: 6
    Last Post: 03-28-2012, 06:46 PM
  3. NumberFormatException
    By Manfizy in forum New To Java
    Replies: 13
    Last Post: 07-09-2009, 11:59 AM
  4. NumberFormatException problem
    By bluebarca in forum New To Java
    Replies: 1
    Last Post: 02-06-2009, 07:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •