Java printf using variable field size?

Solution 1:

Declare an extra variable before using printf:

String format = "%" + fieldSize + "d";
System.out.printf(format, yourVariables);

(this is the first solution I found from a web search)

Solution 2:

This is arguably no better than your "kludge", but here's a method you can embed into the call to printf that will substitute widths for the asterisks that you pass to it. Its result is used as the real format. For example, consider this call:

System.out.printf("%04d %2s %-7d;%n", 65, "a", 6);

I will replace the first and third widths via an embedded function:

System.out.printf( wf("%0*d %2s %-*d;%n", 4, 7), 65, "a", 6);

And here's the code:

public static String wf(String fmt, int... widths) {
    return metaWidthFormat('*', fmt, widths);
}

public static String metaWidthFormat(char wmeta, String fmt, int ... widths) {
    if (fmt == null) return null;
    int wix = 0;
    boolean outside = true;
    // initial capacity is sufficient for each substituted width to be 2 digs
    StringBuilder result = new StringBuilder(fmt.length() + widths.length);
    for (char ch : fmt.toCharArray()) {
        if (outside) {
            result.append(ch);
            outside = (ch != '%');
        } else {
            if (ch == wmeta) {
                result.append(widths[wix++]);
            } else {
                result.append(ch);
            }
            outside = (ch == wmeta) || (ch == '%') || Character.isAlphabetic(ch);
        }
    }
    return result.toString();
}