Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.format() cum vsprintf()

 
Russell Bateman
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Russell Bateman
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Russ
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic