• 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

What standard class prints messages such as "Hello, my name is {1}"?

 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subject says it all. Thanks!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Subject says it all.


Actually, at least for me, it's a bit tricky to interpret just what you're getting at here. Maybe it's me, but I find that it's usually better to supply too much info in questions rather than the opposite.

Perhaps you're looking for the functionality supplied by String.format(....) (or printf as the one calls the other)?

Perhaps you're trying to use another language construct here, to have java print something that is in an ?array? ?on the command line? I'm not sure.

I guess I'm asking you to clarify just what you mean by {1}.

 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I am looking for a standard class that has a method like this:

MyClass.print("Hello, my name is {1}", "Jane");

I don't think printf allows that particular format; I recall someone does.

Thanks.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jane Dodo wrote:I don't think printf allows that particular format; I recall someone does.


If I recall correctly, this syntax works in C#, but I know of no standard Java classes that use this. I could be wrong though.

Printf will allow:


and the String.format equivalent is:

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out MessageFormat. However, it's 0 based, so if you cannot change the formats you must provide dummy values for {0}.
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yup, that's is. MessageFormat. It's pretty neat I see, even allows for choices and string parsing, and has been there for a while, wonder why it's been so obscure?

Also, I found this in 3rd party resource bundles, wonder how they parse it?

validator.length=length must be between {min} and {max}
floor.out.of.range={floor.name} cannot (escaping #{el}) be {out.of.range}
 
reply
    Bookmark Topic Watch Topic
  • New Topic