當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。