23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16. Formatted I/O with java.text<br />

<strong>Java</strong> I/O<br />

One of the most obvious differences between <strong>Java</strong> and C is that <strong>Java</strong> has no equivalent of<br />

printf() or scanf(). Part of the reason is that <strong>Java</strong> doesn't support the variable length<br />

argument lists on which these functions depend. However, the real reason <strong>Java</strong> doesn't have<br />

equivalents to C's formatted I/O routines is a difference in philosophy. C's printf() and the<br />

like combine number formatting with I/O in an inflexible manner. <strong>Java</strong> separates number<br />

formatting and I/O into separate packages and by so doing produces a much more general and<br />

powerful system.<br />

More than one programmer has attempted to recreate printf() and scanf() in <strong>Java</strong>. This<br />

task is difficult, since those functions are designed around variable length argument lists,<br />

which <strong>Java</strong> does not support. However, overloading the + signs for string concatenation is<br />

easily as effective, probably more so, since it doesn't share the problems of mismatched<br />

argument lists. For example, which is clearer to you? This:<br />

printf("%s worked %d hours at $%d per/hour for a total of %d dollars.\n",<br />

hours, salary, hours*salary);<br />

or this:<br />

System.out.println(employee + " worked " + hours + " hours at $" + salary<br />

+ "per/hour for a total of $%d.");<br />

I'd argue that the second is clearer. Among other advantages, it avoids problems with<br />

mismatched format strings and argument lists. (Did you notice that an argument is missing<br />

from the previous printf() statement?) On the flip side, the format string approach is a little<br />

less prone to missing spaces. (Did you notice that the println() statement would print pay<br />

scales as "$5.35per/hour" rather than "$5.35 per/hour"?) However, this is only a cosmetic<br />

problem and is easily fixed. A mismatched argument list in a printf() or scanf() statement<br />

may crash the computer, especially if pointers are involved.<br />

The real advantage of the printf()/scanf() family of functions is not the format string. It's<br />

number formatting:<br />

printf(<br />

"%s worked %4.1d hours at $%6.2d per/hour for a total of %8.2d dollars.\n",<br />

employee, hours, salary, hours*salary);<br />

<strong>Java</strong> 1.0 did not provide classes for specifying the width, precision, and alignment of numeric<br />

strings. <strong>Java</strong> 1.1 and later make these available as subclasses of java.text.NumberFormat.<br />

As well as handling the traditional formatting achieved by languages like C and Fortran,<br />

NumberFormat also internationalizes numbers with different character sets, thousands<br />

separators, decimal points, and digit characters.<br />

16.1 The Old Way<br />

Traditional computer languages have combined input of text with the parsing of numeric<br />

strings. For example, to read a decimal number into the variable x, programmers are<br />

accustomed to writing C code like this:<br />

395

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!