This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In a past life, I could implement a C string-formatting function with multiple, varying (or no) arguments using stdargs.h functionality, no looping and, for example, vsprint(). Please indulge me here: I'm a (very old) C hack trying to mend his ways.
In my new life, I'd like to wrap String.format() that way into a method that accepts multiple, varying (or no) arguments, to wit:
I think the rub is obvious: String.format() is not happy with unmatched conversion specifiers (java.utl.MissingFormatArgumentException). I need a sort of String.vformat() that takes a variable-length array of Objects à la C. My loop here will instead build an Object. Or is there magic glue by which I can move objects from the argument list of buildStatement() to String.format()?
As penance in my new life, I try to resist rolling my own in favor of using existing, tried and true solutions out of standard libraries.
I think you may be making life much harder than it needs to be, because String.format() is pretty much interchangeable with vsprintf. Maybe I'm missing something, though, and you're trying to go after some subtlety that's being lost on me.
The varargs passed to buildStatement are all packaged into an array; the parameter "objects" holds that array. You can pass that array directly to String.format():
I am not at all sure what you're trying to do in your original, though, and I see you've never used the "fmt" argument; don't you really just mean:
And of course, given that, why define "buildStatement()" at all, since String.format() already does the right thing?