当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java MessageFormat formatToCharacterIterator()用法及代码示例


java.text.MessageFormat类的formatToCharacterIterator()方法用于格式化对象数组并将其插入消息格式对象的模式。

用法:

public AttributedCharacterIterator formatToCharacterIterator(Object arguments)

参数:此方法将对象数组作为要进行格式化的参数。


返回值:此方法返回将描述格式值的属性字符迭代器。

异常:如果参数为null,则此方法引发NullPointerException。

下面是说明formatToCharacterIterator()方法的示例:

范例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); 
        } 
    } 
}
输出:
formatted array:4, 4.23, 4.235

范例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); 
        } 
    } 
}
输出:
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-



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 MessageFormat formatToCharacterIterator() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。