Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java printf using variable field size?

I'm just trying to convert some C code over to Java and I'm having a little trouble with String.printf.

In C, to get a specific width based on a variable, I can use:

printf("Current index = %*d\n", sz, index); 

and it will format the integer to be a specific size based on sz.

Trying:

System.out.println(String.format("Current index = %*d\n", sz, index)); 

results in an error since it doesn't like the *.

I currently have the following kludge:

System.out.println(String.format("Current index = %" + sz + "d\n", index)); 

but I'm hoping there's a slightly better way, yes?

like image 391
paxdiablo Avatar asked Mar 31 '10 02:03

paxdiablo


People also ask

Can you use a variable in printf Java?

printf . In C, to get a specific width based on a variable, I can use: printf("Current index = %*d\n", sz, index); and it will format the integer to be a specific size based on sz .

What is %s in Java printf?

To use the Java String printf method to format output text, follow these rules: Use %s as the printf String specifier in the text string being formatted. Use %S as the printf String specifier to output upper-case text. Precede the letter s with a number to specify field width.

How does %D work in Java?

%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.

What is %d in Java printf?

The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: The value of i is: 461012. The printf and format methods are overloaded.


2 Answers

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)

like image 172
user1970914 Avatar answered Oct 02 '22 09:10

user1970914


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(); } 
like image 27
Como Avatar answered Oct 02 '22 08:10

Como