Traditional computer languages have combined input of text with the parsing of numeric strings. For example, to read a decimal number into the variable x, programmers are accustomed to writing C code like this:
In C++, that line would become:
cin >> x;
READ 2, X 2 FORMAT (F5.1)
Similarly, formatting numeric strings for output tends to be mixed up with writing the string to the screen. For instance, consider the simple task of writing the double variable salary with two decimal digits of precision. In C, youd write this:
cout.precision(2); cout << salary;
PRINT 20, SALARY 20 FORMAT(F10.2)
This conflation of basic input and output with number formatting is so ingrained in many programmers that we rarely stop to think whether it actually makes sense. What, precisely, does the formatting of numbers as text strings have to do with input and output? Its certainly true that you often need to format numbers to print numbers on the console, but you also need to format numbers to write data in files, to include numbers in text fields and text areas, and to send data across the network. What makes the console so special that it has to have a group of number-formatting routines all to itself? In C, the printf( ) and scanf( ) functions are supplemented by fprintf( ) and fscanf( ) for formatted I/O to files and by sprintf( ) and sscanf( ) for formatted I/O to strings. Perhaps the conflation of I/O with number formatting is really a relic of a time when command-line interfaces were a lot more important than they are today, and its simply that nobody thought to challenge this assumption, at least until Java. When you think about it, theres no fundamental connection between converting a binary number like 11010100110110100100011101011011 to a text string like " -7.500E+12" and writing that string onto an output stream. These are two different operations, and in Java they e handled by separate classes. Input and output are handled by all the streams and readers and writers Ive been discussing while number formatting is handled by a few NumberFormat classes from the java.text package Ill introduce in this chapter.
In Java you don say, "Print the double variable salary 12 places wide with three decimal places of precision." Instead, you say "First, make a string from the double variable salary 12 places wide with three decimal places of precision. Then print that string." Similarly, when doing input, you first read the string, then convert it to a number. You don read the number directly. This really isn very different from the programs you e used to writing in other languages; it adds a step, but the benefit is enhanced flexibility, particularly in regard to internationalization. Its easy to add new NumberFormat classes and locales that handle different kinds of formatting.
In this chapter, well explore how to use the java.text.NumberFormat and java.text.DecimalFormat classes to format integers and floating-point numbers. Youll also learn how the java.util.Locale class lets you select number formats matched to different languages, cultures, and countries. Theres more in the java.text package I won cover. In particular, java.text includes classes for formatting dates, and sorting and collating text. These classes can also be customized to different locales. The date formats in particular work very similarly to the number formats discussed in this chapter and should be easy to pick up from the API documentation once you understand NumberFormat.
Streams in Memory
The File System
Working with Files
File Dialogs and Choosers
Character Sets and Unicode
Readers and Writers
Formatted I/O with java.text
The Java Communications API
The J2ME Generic Connection Framework
Authors: Elliotte Rusty Harold
Similar book on Amazon
PostgreSQL(c) The comprehensive guide to building, programming, and administering PostgreSQL databases