Code example for Formatter

Methods: format

0
 
    public static void main(String [] args) {
        StockName sn = new StockName("HUGE", "Huge Fruit, Inc.",
                                     "Fruit Titanesque, Inc."); 
        CharBuffer cb = CharBuffer.allocate(128);
        Formatter fmt = new Formatter(cb);
 
        fmt.format("%s", sn);            //   -> "Huge Fruit, Inc."
        test(cb, "Huge Fruit, Inc.");
 
        fmt.format("%s", sn.toString()); //   -> "HUGE - Huge Fruit, Inc."
        test(cb, "HUGE - Huge Fruit, Inc.");
 
        fmt.format("%#s", sn);           //   -> "HUGE"
        test(cb, "HUGE");
 
        fmt.format("%-10.8s", sn);       //   -> "HUGE      "
        test(cb, "HUGE      ");
 
        fmt.format("%.12s", sn);         //   -> "Huge Fruit,*"
        test(cb, "Huge Fruit,*");
 
        fmt.format(Locale.FRANCE, "%25s", sn);
                                         //   -> "   Fruit Titanesque, Inc." 
        test(cb, "   Fruit Titanesque, Inc.");
    } 
 
    private static void test(CharBuffer cb, String exp) {