Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 270—Application Development II Chapter 28—Formatted Output.

Similar presentations


Presentation on theme: "CIS 270—Application Development II Chapter 28—Formatted Output."— Presentation transcript:

1 CIS 270—Application Development II Chapter 28—Formatted Output

2 2 23.1 Introduction Method printf formats and outputs data to the standard output stream— _____________. Class Formatter does likewise to a specified destination, such as a string or a file output stream.

3 3 28.2 Streams A stream is a sequence of _______. For input, streams flow from a device to main memory. For output, streams flow from main memory to a device. Three automatic Java streams: System.in —connected to the keyboard System.out —connected to the screen System.err —connected to the screen, for error messages

4 4 28.3 Formatting Output with printf printf( format-string, argument-list ); format-string describes the output format can contain fixed text and format ___________ a format specifier, such as f, is a placeholder for a value a format specifier begins with % and is followed by a conversion character, such as %f optional formatting information is specified between the % and the conversion character, such as %.2f The argument-list contains literals or _________ that correspond in number and type to the conversion characters in the format-string. The argument index (such as 1$ ) can specify argument to which formatting will apply: %1$.2f

5 5 28.4 Printing Integers An integer is a + or – whole number with no decimal point. Integral conversion characters d for decimal (base 10) integers (e.g., 26) o for _______ (base 8) integers (e.g., 26  32) x or X for hexidecimal (base 16) integers (e.g. 26  1A) The printf method Format: printf( format-string, argument-list ); Example: int aNumber = 26; System.out.printf( “%d\n”, aNumber);

6 6 28.5 Printing Floating-Point Numbers A floating-point value contains a decimal point. Floating-point conversion characters e or E for ____________ notation f for for floating-point, decimal format g or G for floating-point or exponential format depending on the magnitude of the value ( e for 10 7 ) a or A for floating-point in hexidecimal format Examples System.out.printf( “%e\n”, 12345678.9); Result: 1.2345678e+07 System.out.printf( “%f\n”, 12345678.9); Result: 12345678.900000

7 7 28.6 Printing Strings and Characters Character and string conversion characters c or C for characters (lower and upper case, respectively) s or S for strings (lower and upper case, respectively) Using s can convert an _________ object to a string. Formatting characters cannot convert a string to a character.

8 8 28.7 Printing Dates and Times 1 The conversion characters t or T can print dates and times. The conversion ________ character specifies formats. Examples: %tc for complete date and time %tF for year-month-date format %tr for hour:minute:second AM|PM format %tA for the full day of the week (e.g., Wednesday) %tp for morning or afternoon marker in lower case (pm)

9 9 28.7 Printing Dates and Times 2 Application for dates and times Calendar today = Calendar.getInstance(); System.out.printf(“%1$tA, %1$tB %1$td, %1$tY\n”, today); Result: Wednesday, August 03, 2005

10 10 28.9 Printing with Field Widths and Precision An integer right of % is the maximum field width ( + for right justification, - for left justification) A. to the right of %, followed by a positive integer, is the number of decimal places to be displayed. Example: printf( “%9.3f”, 123.456789 ); Right justifies ( +, implied) using a __________ field width of 9 with 3 decimal places. Result: 123.457

11 11 28.10 Using Flags A flag is placed just to the right of the % Flags - to left justify + to display a + for positive values and – for negative values 0 to pad a field with leading zeros, to use the locale-specific thousands separator ( to enclose negative numbers in parentheses

12 12 28.13 Class Formatter System.out.printf() sends output to the standard output stream (screen). The class Formatter (in package ___________ ) allows sending formatted output to a specified destination. Example Formatter formatter = new Formatter(); formatter.format(“%d = %#o = %#X, 10, 10, 10); JOptionPane.showMessageDialog(null, formatter.toString()); Also, String x = String.format (“%d = %#o = %#X, 10, 10, 10);


Download ppt "CIS 270—Application Development II Chapter 28—Formatted Output."

Similar presentations


Ads by Google