Yanick Rochon Yanick Rochon - 2 months ago 16
Java Question

Java format array (using the Formatter class)

We have method that looks like this :

public String getCommandString(Object...args) {
return this.code + " " + String.format(this.pattern, args);


This method is usually called like this :

// cmd.code = 20
// cmd.pattern = "%1$s = %2$d"
String str = cmd.getCommandString("foo", 3); // -> "20 foo = 3"

and other string patterns (this is for a very simple text-based server-client program)

Now, is it possible for pattern to take into account a variable number of arguments, such as

// cmd.code = 20
// cmd.pattern = ???
String s1 = cmd.getCommandString("a", "b", "c"); // -> 20 a b c
String s2 = cmd.getCommandString("Hello", "world"); // -> 20 Hello world
String s3 = cmd.getCommandString("1", "2, "3", "4", "5", "6"); // -> 20 1 2 3 4 5 6

Assuming perhaps that each argument is of the same type (strings)? Or do I have to override the method and format the string manually? More specifically, I'm looking for a generic string pattern to format a variable number of arguments (of the same type). I remember making such a thing in C, but is this possible in Java?


The functionality you are asking for, if I understand it correctly, does not exist. Here is the javadoc section from the Formatter class:

Format specifiers can reference arguments in three ways:

Explicit indexing is used when the format specifier contains an argument index. The argument index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc. An argument may be referenced more than once. For example:

formatter.format("%4$s %3$s %2$s %1$s %4$s %3$s %2$s %1$s", "a", "b", "c", "d") // -> "d c b a d c b a"

Relative indexing is used when the format specifier contains a '<' ('\u003c') flag which causes the argument for the previous format specifier to be re-used. If there is no previous argument, then a MissingFormatArgumentException is thrown.

formatter.format("%s %s % "a b b b" // "c" and "d" are ignored because they are not referenced

Ordinary indexing is used when the format specifier contains neither an argument index nor a '<' flag. Each format specifier which uses ordinary indexing is assigned a sequential implicit index into argument list which is independent of the indices used by explicit or relative indexing.

formatter.format("%s %s %s %s", "a", "b", "c", "d") // -> "a b c d"

It is possible to have a format string which uses all forms of indexing, for example:

formatter.format("%2$s %s % "b a a b" // "c" and "d" are ignored because they are not referenced

The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. If the argument index is does not correspond to an available argument, then a MissingFormatArgumentException is thrown.

If there are more arguments than format specifiers, the extra arguments are ignored.

What you are asking for is a way to construct a pattern that will accept an arbitrary number of arguments and use tham all. The problem is that the Formatter class can only associate format specifiers with arguments either explicitly, relatively, or ordinarily. In each of these cases, the number of forma specifiers is fixed. There is no construct in which you can repeat or loop a format specifier. The relative technique looks promising, but there is no way to nest, or loop, it.