📌  相关文章
📜  Java中的 MessageFormat formatToCharacterIterator() 方法与示例

📅  最后修改于: 2022-05-13 01:55:40.003000             🧑  作者: Mango

Java中的 MessageFormat formatToCharacterIterator() 方法与示例

Java.text.MessageFormat类的formatToCharacterIterator()方法用于格式化对象数组并将它们插入到消息格式对象的模式中。
句法:

public AttributedCharacterIterator formatToCharacterIterator(Object arguments)

参数:此方法将对象数组作为将进行格式化的参数。
返回值:此方法返回将描述格式化值的属性字符迭代器。
异常:如果参数为空,此方法将抛出NullPointerException
以下是说明formatToCharacterIterator()方法的示例:
示例 1:

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


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


输出:
formatted array : 4, 4.23, 4.235

示例 2:

Java

// 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) };
 
            // Formatting 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);
        }
    }
}
输出:
argument is null 
Exception thrown : java.lang.NullPointerException: formatToCharacterIterator must be passed non-null object

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