Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

MessageFormat formatToCharacterIterator() method in Java with Example

  • Last Updated : 17 Jan, 2020

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:




// 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);
        }
    }
}
Output:
formatted array : 4, 4.23, 4.235

Example 2:




// 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);
        }
    }
}
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-




My Personal Notes arrow_drop_up
Recommended Articles
Page :