MessageFormat formatToCharacterIterator() method in Java with Example

The formatToCharacterIterator() method of java.text.MessageFormat class is used to format an array of object and insert them into the pattern of message format object.

Syntax:

public AttributedCharacterIterator formatToCharacterIterator(Object arguments)

Parameter: This method takes array of object as an parameter over which formatting is going to take place.

Return Value: This method returns attribute character iterator which will describe the formatted value .

Exception: This method throws NullPointerException if argument is null.



Below are the examples to illustrate the formatToCharacterIterator() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// formatToCharacterIterator() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing new MessageFormat Object
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {0, number, #.##}, {0, number}");
  
            // Creating and initializing an array of type Double
            // to be formated
            Object[] objs = { new Double(4.234567) };
  
            // Formating an array of object
            // using formatToCharacterIterator() method
            CharacterIterator str = mf.formatToCharacterIterator(objs);
  
            // display the result
            System.out.print("formatted array : ");
            System.out.print(str.first());
            for (int i = 0; i <= str.getEndIndex() - 2; i++)
                System.out.print(str.next());
        }
        catch (NullPointerException e) {
            System.out.println("pattern is null ");
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

formatted array : 4, 4.23, 4.235

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// formatToCharacterIterator() method
  
import java.text.*;
import java.util.*;
import java.io.*;
  
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing new MessageFormat Object
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {0, number, #.##}, {0, number}");
  
            // Creating and initializing an array of type Double
            // to be formated
            Object[] objs = { new Double(4.234567) };
  
            // Formating an array of object
            // using formatToCharacterIterator() method
            CharacterIterator str = mf.formatToCharacterIterator(null);
  
            // display the result
            System.out.print("formatted array : ");
            System.out.print(str.first());
            for (int i = 0; i <= str.getEndIndex() - 2; i++)
                System.out.print(str.next());
        }
        catch (NullPointerException e) {
            System.out.println("argument is null ");
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

argument is null 
Exception thrown : java.lang.NullPointerException: formatToCharacterIterator must be passed non-null object

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#formatToCharacterIterator-java.lang.Object-

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.