Java The Complete Reference, Seventh Edition
Theformat( )method accepts a wide variety of format specifiers, which are shown in Table 18-12. Notice that many specifiers have ...
Chapter 18: java.util Part 2: More Utility Classes 529 // A very simple example that uses Formatter. import java.util.*; class F ...
530 Part II: The Java Library It produces the following output: 1000.000000 1000.000000 100000.000000 1000.000000 100000.000000 ...
// Display standard 12-hour time format. fmt.format("%tr", cal); System.out.println(fmt); // Display complete time and date info ...
The %n and %% Specifiers The%nand%%format specifiers differ from the others in that they do not match an argument. Instead, they ...
Chapter 18: java.util Part 2: More Utility Classes 533 Specifying a Minimum Field Width An integer placed between the%sign and t ...
534 Part II: The Java Library fmt.format("%4d %4d %4d", i, i*i, i*i*i); System.out.println(fmt); } } } Its output is shown here: ...
Chapter 18: java.util Part 2: More Utility Classes 535 fmt.format("%.15s", "Formatting with Java is now easy."); System.out.prin ...
536 Part II: The Java Library fmt.format("|%-10.2f|", 123.123); System.out.println(fmt); } } It produces the following output: | ...
The output is shown here: -100 100 -200 200 Notice that the positive values have a leading space, which causes the digits in the ...
538 Part II: The Java Library Specifier Effect %E Causes theesymbol that indicates the exponent to be displayed in uppercase. %G ...
In this example, the first format specifier matches 30, the second matches 10, and the third matches 20. Thus, the arguments are ...
540 Part II: The Java Library Scanner Scanneris the complement ofFormatter. Added by JDK 5,Scannerreads formatted input and conv ...
Chapter 18: java.util Part 2: More Utility Classes 541 This next line creates aScannerthat reads from standard input, which is t ...
542 Part II: The Java Library Method Description boolean hasNext( ) Returnstrueif another token of any type is available to be r ...
Thewhileloop stops as soon as the next token is not an integer. Thus, the loop stops reading integers as soon as a non-integer i ...
544 Part II: The Java Library Some Scanner Examples The addition ofScannerto Java makes what was formerly a tedious task into an ...
Chapter 18: java.util Part 2: More Utility Classes 545 Notice that the numbers are read by callingnextDouble( ). This method rea ...
546 Part II: The Java Library You can useScannerto read input that contains several different types of data—even if the order of ...
Chapter 18: java.util Part 2: More Utility Classes 547 int: 10 double: 12.2 String: one boolean: true String: two boolean: false ...
«
24
25
26
27
28
29
30
31
32
33
»
Free download pdf