Hi all, I wonder if you can help me to understand this. I came across the following code:

@Override
   public String toString()
   {
      return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", 
         super.toString(), "hourly wage", getWage(), 
         "hours worked", getHours() );
   } // end method toString

I have some problems with the various colons and semicolons inside the String.format method, here return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f". What are they? I dont' think they are flags or format specifiers are they? Are they just literals?
I have done a bit of reading about the subject but I haven't found that much on the book I am reading, so I thought I'd ask. Now, the semicolons and colons get printed, but is there a general rule as to what they are, how they work and where they go?
thanks

Recommended Answers

All 4 Replies

They're simple literals. Just like the "h", "o", "u" etc at the start of the string. Anythingthat's not part of a "format specifier" is"fixed text" (ie a literal).
See the API doc for the Formatter class for complete detailed descriptions of all the format string options.

ah ok thanks for clarifying that! Ah, of course, I didn't think about looking in the api!
cheers

If that's it then please mark this "solved" for our knowledge base.
Thanks
J

sorry, I forgot to make it solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.