3

We have method that looks like this :

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

Where this.code:int and this.pattern:String.

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?

3
  • I don't get it. Can't you just concatenate everything? (and if you have a pattern, just run each string through the pattern and concatenate the results) Commented Oct 15, 2011 at 3:38
  • Yes for the specific example cited, calling StringUtils.join(args, ' ') is the way to go, but the I think the OP was just using that as an example. :) Commented Oct 15, 2011 at 3:42
  • I am aware of third party classes to manipulate objects (such as StringUtils) but usually turn to the java.* packages first, if I can. Commented Oct 15, 2011 at 4:29

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping for some nifty undocumented tricks, but looks like this will not work. I'll make due using other methods then.
-1

Use this

public String getCommandString(Object...args) {
    // Want to have a "-" if args are not given
    return this.code + " " + args != null && args.length > 0
            ? Arrays.stream(args).map(o -> o.toString()).collect(Collectors.joining(" + ")) : "-";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.