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

   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?

3 Years
Discussion Span
Last Post by Violet_82

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.