This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What standard class prints messages such as Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What standard class prints messages such as "Hello, my name is {1}"?" Watch "What standard class prints messages such as "Hello, my name is {1}"?" New topic
Author

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

Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
Subject says it all. Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Mar 28, 2009
Posts: 145
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

Joined: Feb 23, 2007
Posts: 1561
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:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Check out MessageFormat. However, it's 0 based, so if you cannot change the formats you must provide dummy values for {0}.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145

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}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What standard class prints messages such as "Hello, my name is {1}"?