Java Reference
In-Depth Information
TryFormattedOutput.java
This produces the following output:
The string is:
The quick brown fox.
The quick brown fox.
The first instance of
str
in the output is produced by the
"%s"
specification that follows the first
"%n"
,
and the second instance is produced by the
"%1$25s"
specification. The
"%1$25s"
specification has a field
width that is greater than the length of the string so the string appears right-justified in the output field. You
could apply the
'-'
flag to obtain the string left-justified in the field.
You have many more options and possibilities for formatted output. Try experimenting with them your-
self, and if you want details of more specifier options, read the JDK documentation for the
printf()
meth-
od in the
PrintStream
class.
The Locale Class
You can pass an object of type
java.util.Locale
as the first argument to the
printf()
method, preceding
the format string and the variable number of arguments that you want displayed. The
Locale
object specifies
a language or a country + language context that affects the way various kinds of data, such as dates or mon-
etary amounts, is presented.
You have three constructors available for creating
Locale
objects that accept one, two, or three arguments
of type
String
. The first argument specifies a language as a string of two lowercase letters representing a
Language Code defined by the standard ISO-639. Examples of language codes are
"fr"
for French,
"en"
for English, and
"be"
for Belarusian. The second argument specifies a country as a string of two upper-
case letters representing a country code defined by the ISO-3166 standard. Examples of country codes are
"US"
for the USA,
"GB"
for the United Kingdom, and
"CA"
for Canada. The third argument is a vendor or
browser-specific code such as
"WIN"
for Windows or
"MAC"
for Macintosh.
However, rather than using a class constructor, more often than not you use one of the
Locale
class
constant static data members that provide predefined
Locale
objects for common national contexts. For ex-
ample, you have members
JAPAN
,
ITALY
, and
GERMANY
for countries and
JAPANESE
,
ITALIAN
, and
GERMAN
for the corresponding languages. Consult the JDK documentation for the
Locale
class for a complete list of
these.
Formatting Data into a String
The
printf()
method produces the string that is output by using an object of type
java.util.Formatter
,
and it is the
Formatter
object that is producing the output string from the format string and the argument
values. A
Formatter
object is also used by a static method
format()
that is defined in the
String
class,
and you can use this method to format data into a string that you can use wherever you like — for displaying
data in a component in a windowed application, for example. The static
format()
method in the
String
class comes in two versions, and the parameter lists for these are the same as for the two versions of the
printf()
method in the
PrintStream
class just discussed, one with the first parameter as a
Locale
object
followed by the format string parameter and the variable parameter list and the other without the
Locale
parameter. Thus, all the discussion of the format specification and the way it interacts with the arguments
you supply applies equally well to the
String.format()
method, and the result is returned as type
String
.