NumberFormat parseObject() method in Java with Examples

The parseObject() method is a built-in method of the java.text.NumberFormat which parses a text from a string to produce a Number. The function attempts to parse text starting at a given index. When parsing occurs, the given index is set to the last character used, in case parsing fails, the given index is not changed and the error index is set to the index where the error occurs.

Syntax:

public final Object parseObject(String source, ParsePosition pos)

Parameters: The function accepts two parameters which are described below:

  • source: specifies the string to be parsed
  • pos: specifoes the ParsePosition object with index and error index information as described

Return Value: The function returns a boolean value, it returns true if numbers can be parsed as integers, else it returns false.



Exceptions: The function throws a NullPointerException if source or pos is initialized as null.

Below is the implementation of the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
  
import java.text.NumberFormat;
import java.util.Locale;
import java.text.ParsePosition;
  
public class Main {
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the instance
        NumberFormat nF
            = NumberFormat.getNumberInstance();
  
        // Prints the parsed number or NULL
        System.out.println(nF
                               .parseObject("456",
                                            new ParsePosition(0)));
    }
}

chevron_right


Output:

456

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
  
import java.text.NumberFormat;
import java.util.Locale;
import java.text.ParsePosition;
  
public class Main {
    public static void main(String[] args)
        throws Exception
    {
        try {
            // Get the instance
            NumberFormat nF
                = NumberFormat.getNumberInstance();
  
            // Prints the parsed number or NULL
            System.out.println(
                nF
                    .parseObject(null,
                                 new ParsePosition(0)));
        }
        catch (Exception e) {
            System.out.println("Exception: "
                               + e);
        }
    }
}

chevron_right


Output:

Exception: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#parseObject(java.lang.String, java.text.ParsePosition)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.