MyFormattableObject.java Source code

Java tutorial

Introduction

Here is the source code for MyFormattableObject.java

Source

import java.util.Formattable;
import java.util.FormattableFlags;
import java.util.Formatter;

class MyFormattableObject implements Formattable {
    public void formatTo(Formatter formatter, int flags, int width, int precision) {
        int alternateFlagValue = FormattableFlags.ALTERNATE & flags;
        if (alternateFlagValue == FormattableFlags.ALTERNATE) {
            System.out.println("FormattableFlags.ALTERNATE");
        }
        int upperFlagValue = FormattableFlags.UPPERCASE & flags;
        if (upperFlagValue == FormattableFlags.UPPERCASE) {
            System.out.println(FormattableFlags.UPPERCASE);
        }
        int leftJustifiedFlagValue = FormattableFlags.LEFT_JUSTIFY & flags;
        if (leftJustifiedFlagValue == FormattableFlags.LEFT_JUSTIFY) {
            System.out.println("Left-justified flag '-' is used");
        } else {
            System.out.println("Left-justified flag '-' is not used");
        }

        formatter.format("value");
    }
}

public class Main {
    public static void main(String[] args) {
        MyFormattableObject fp = new MyFormattableObject();
        System.out.printf("%s %n", fp);
        System.out.printf("%#s %n", fp);
        System.out.printf("%S %n", fp);
        System.out.printf("%#S %n", fp);

    }
}