• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.format() cum vsprintf()

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic