I noticed the table of contents from your book includes a chapter on "formatted IO". I'm not familiar with this aspect of 1.5 - care to give a summary of what it's like, and maybe talk about the likely useful applications for it that your book covers in greater detail?
Warren: I am glad you asked! Formatted I/O is one aspect of the "new Java" that many programmers will really like. To begin, here are the opening paragraphs from my Formatted I/O Chapter:
Java 2, v5.0 has added a capability that has long been desired by programmers: the ability to easily create formatted output. Although Java has always offered a rich and varied API, it had not offered an easy way to create formatted text output, especially for numeric values. Although classes such as NumberFormat, DateFormat, and MessageFormat provided by earlier versions of Java do provide useful formatting capabilities, they were not especially convenient. Furthermore, unlike C and C++, which support the widely understood and used printf() family of functions that offer a simple way to format output, Java had previously not offered such methods. One reason for this is that printf-style formatting requires the use of variable length arguments (varargs), which Java did not support until the release of Java 2, v5.0. Once varargs were available, it was a simple matter to add a general-purpose formatter.
Java 2, v5.0 also adds support for reading formatted input. Although it has always been possible to read formatted input, it required more effort than many programmers would prefer. Now it is easy to read all types of numeric values, strings, and other types of data, whether it comes from a disk file, the keyboard, or another source.
To support formatted I/O, Java 2, v5 adds the Formatter and Scanner classes. It also adds the printf() method to both PrintStream and PrintWriter.
Honestly, it isn't possible to even begin to describe these features in this forum because of the many, many options they support. However, to give you can idea of their power, here is a very short program that demonstrates the Formatter class.
The output is shown here:
Formatting numbers is easy 10 98.60 123,456,789
Formatter operates by matching conversion specifiers, such as %d (integer), %s (string), and %f (floating-point), that are embedded into the format string that constitutes the first parameter to format(), to their corresponding arguments in the remainder of the argument list (which is a varargs list). Thus, the %s matches the string "numbers", the %4.2f matches 98.6, and %,d matches 123456789.
In the example, the conversion specifier %5.2f causes the corresponding value to be displayed in a field that is at least 5 characters wide and to use 2 decimal places. The %,d specifier causes commas to be added to the number when it is displayed.
There is much, much, much more to Formatter that this simple example shows, but it gives you some idea of its power.
Of course, you don't have to use Formatter directly. You can simply call [/b]printf()[/b] (which automatically uses Formatter). For example, here is the preceding program rewritten to use printf().
For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Joined: Mar 04, 2004
Thanks! Looks kind of familiar to us old c people ... but it should definitely make setting up formatted output much quicker, if not more elegant.