Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes String.format() cum vsprintf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String.format() cum vsprintf()" Watch "String.format() cum vsprintf()" New topic

String.format() cum vsprintf()

Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
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.

Thanks in advance for any help you can give me,

Russ Bateman
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

I may be missing something obvious here... but String.format already accepts variable arguments. The protocol is:
public static String format(String format, Object... args)

Does that not already do what you want?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Russ,

Welcome to JavaRanch!

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?

[Jess in Action][AskingGoodQuestions]
Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
Oops, I was simplifying code and renamed stmt to fmt in one place. Sorry about the miscue.

Okay, this does turn out to be the solution (and I've already tested it)...

...which I had not envisioned precisely under the influence of how this works in C. My short-sightedness.

Thanks for your quick responses.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
There is an even simpler way to do itA few links: Java™ Tutorials about "formatting" String#format(java.lang.String, java.lang.Object...) method, where there is this "details" link to the Formatter class, where you will find a complete list of the % tags.
I agree. Here's the link:
subject: String.format() cum vsprintf()
It's not a secret anymore!