Presentation is loading. Please wait.

Presentation is loading. Please wait.

 1992-2007 Pearson Education, Inc. All rights reserved. 1 29 Formatted Output.

Similar presentations


Presentation on theme: " 1992-2007 Pearson Education, Inc. All rights reserved. 1 29 Formatted Output."— Presentation transcript:

1  1992-2007 Pearson Education, Inc. All rights reserved. 1 29 Formatted Output

2  1992-2007 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn:  To understand input and output streams.  To use printf formatting.  To print with field widths and precisions.  To use formatting flags in the printf format string.  To print with an argument index.  To output literals and escape sequences.  To format output with class Formatter.

3  1992-2007 Pearson Education, Inc. All rights reserved. 3 29.1 Introduction 29.2 Streams 29.3 Formatting Output with printf 29.4 Printing Integers 29.5 Printing Floating-Point Numbers 29.6 Printing Strings and Characters 29.7 Printing Dates and Times 29.8 Other Conversion Characters 29.9 Printing with Field Widths and Precisions 29.10 Using Flags in the printf Format String 29.11 Printing with Argument Indices 29.12 Printing Literals and Escape Sequences 29.13 Formatting Output with Class Formatter 29.14 Wrap-Up

4  1992-2007 Pearson Education, Inc. All rights reserved. 4 29.1 Introduction Method printf – Formats and outputs data to the standard output stream, System.out Class Formatter – Formats and outputs data to a specified destination E.g., a string or a file output stream – Use as an alternative to the printf method

5  1992-2007 Pearson Education, Inc. All rights reserved. 5 29.2 Streams Streams – Sequences of bytes – Can often be redirected Standard input – keyboardSystem.in Standard output – screenSystem.out Standard error – screenSystem.err More in Chapters 14 and 24

6  1992-2007 Pearson Education, Inc. All rights reserved. 6 29.3 Formatting Output with printf printf – Precise output formatting Conversion specifications: flags, field widths, precisions, etc. – Can perform rounding aligning columns right/left justification inserting literal characters exponential format octal and hexadecimal format fixed width and precision date and time format

7  1992-2007 Pearson Education, Inc. All rights reserved. 7 29.3 Formatting Output with printf (Cont.) Format String – Describe the output format – Consist of fixed text and format specifier Format specifier – Placeholder for a value – Specify the type of data to output – Begins with a percent sign ( % ) and is followed by a conversion character E.g., %s, %d – Optional formatting information Argument index, flags, field width, precision Specified between % and conversion character

8  1992-2007 Pearson Education, Inc. All rights reserved. 8 29.4 Printing Integers Integer – Whole number (no decimal point): 25, 0, -9 – Positive, negative, or zero – Only minus sign prints by default Format – printf( format-string, argument-list ); – format-string Describes the output format – argument-list Contains the values that corresponding to the format specifiers

9  1992-2007 Pearson Education, Inc. All rights reserved. 9 Fig. 29.1 | Integer conversion characters.

10  1992-2007 Pearson Education, Inc. All rights reserved. 10

11  1992-2007 Pearson Education, Inc. All rights reserved. 11 29.5 Printing Floating-Point Numbers Floating Point Numbers – Have a decimal point ( 33.5 ) – Computerized scientific notation (exponential notation) 150.4582 is 1.504582 x 10² in scientific 150.4582 is 1.504582e+02 in exponential ( e stands for exponent) use e or E – f – print floating point with at least one digit to left of decimal – g (or G ) - prints in f or e ( E ) Use exponential if the magnitude is less than 10 -3, or greater than or equal to 10 7

12  1992-2007 Pearson Education, Inc. All rights reserved. 12 Fig. 29.3 | Floating-point conversion characters.

13  1992-2007 Pearson Education, Inc. All rights reserved. 13

14  1992-2007 Pearson Education, Inc. All rights reserved. 14 29.6 Printing Strings and Characters Strings are objects, characters (type char) are a primitive data type in Java Conversion character c and C – Require char – C (capital C) displays the output in uppercase letters Conversion character s and S – String – Object Implicit use of object’s toString method – S (capital S) displays the output in uppercase letters

15  1992-2007 Pearson Education, Inc. All rights reserved. 15 Common Programming Error 29.1 Using % c to print a string causes an IllegalFormatConversionException A string cannot be converted to a character.

16  1992-2007 Pearson Education, Inc. All rights reserved. 16

17  1992-2007 Pearson Education, Inc. All rights reserved. 17 29.7 Printing Dates and Times Conversion characters t and T – Print dates and times in various formats – Followed by a conversion suffix character – Require the corresponding argument to be of type long, Long, Calendar or Date Conversion suffix characters – Specify the date and/or time format – Format date and time compositions – Format date – Format time

18  1992-2007 Pearson Education, Inc. All rights reserved. 18 Fig. 29.6 | Date and time composition conversion suffix characters.

19  1992-2007 Pearson Education, Inc. All rights reserved. 19 Fig. 29.7 | Date formatting conversion suffix characters.

20  1992-2007 Pearson Education, Inc. All rights reserved. 20 Fig. 29.8 | Time formatting conversion suffix characters.

21  1992-2007 Pearson Education, Inc. All rights reserved. 21

22  1992-2007 Pearson Education, Inc. All rights reserved. 22

23  1992-2007 Pearson Education, Inc. All rights reserved. 23 29.8 Other Conversion Characters Remaining conversion characters – b or B boolean or Boolean value – h or H String representation of an object’s hash code in hexadecimal format – % Percent character ( % ) – n Platform-specific line separator – \r\n on Windows – \n on UNIX\Linux

24  1992-2007 Pearson Education, Inc. All rights reserved. 24 Fig. 29.10 | Other conversion specifiers.

25  1992-2007 Pearson Education, Inc. All rights reserved. 25

26  1992-2007 Pearson Education, Inc. All rights reserved. 26 29.9 Printing with Field Widths and Precisions Field width – Size of field in which data is displayed – If width larger than data, default right justified If field width too small, increases to fit data Minus sign uses one character position in field – Integer width inserted between % and conversion specifier E.g., %4d – field width of 4 – Can be used with all format specifiers except the line separator ( %n )

27  1992-2007 Pearson Education, Inc. All rights reserved. 27 29.9 Printing with Field Widths and Precisions (Cont.) Precision – Meaning varies depending on data type: Floating point – Number of digits to appear after decimal ( e or E and f ) – Maximum number of significant digits ( g or G ) Strings – Maximum number of characters to be written from string – Format Use a dot (. ) then precision number after % e.g., %.3f

28  1992-2007 Pearson Education, Inc. All rights reserved. 28 29.9 Printing with Field Widths and Precisions (Cont.) Field width and precision – Can both be specified %width.precision %5.3f – Negative field width – left justified – Positive field width – right justified – Precision must be positive Example: printf( "%9.3f", 123.456789 );

29  1992-2007 Pearson Education, Inc. All rights reserved. 29 Common Programming Error 29.3 Not providing a sufficiently large field width to handle a value to be printed can off­set other data being printed and produce confusing outputs. Know your data!

30  1992-2007 Pearson Education, Inc. All rights reserved. 30

31  1992-2007 Pearson Education, Inc. All rights reserved. 31

32  1992-2007 Pearson Education, Inc. All rights reserved. 32 29.10 Using Flags in the printf Format String Flags – Supplement formatting capabilities – Place flag immediately to the right of percent sign – Several flags may be combined

33  1992-2007 Pearson Education, Inc. All rights reserved. 33 Fig. 29.14 | Format string flags.

34  1992-2007 Pearson Education, Inc. All rights reserved. 34

35  1992-2007 Pearson Education, Inc. All rights reserved. 35

36  1992-2007 Pearson Education, Inc. All rights reserved. 36

37  1992-2007 Pearson Education, Inc. All rights reserved. 37 Use the # flag to prefix 0 to the octal value and 0x to the hexadecimal value

38  1992-2007 Pearson Education, Inc. All rights reserved. 38

39  1992-2007 Pearson Education, Inc. All rights reserved. 39

40  1992-2007 Pearson Education, Inc. All rights reserved. 40

41  1992-2007 Pearson Education, Inc. All rights reserved. 41 29.11 Printing with Argument Indices Argument index – Optional decimal integer followed by a $ sign – Indicate the position of the argument in the argument list E.g., 1$ -- first argument – Usage Reorder the output Avoid duplicating arguments Reuse same argument multiple times

42  1992-2007 Pearson Education, Inc. All rights reserved. 42

43  1992-2007 Pearson Education, Inc. All rights reserved. 43 29.12 Printing Literals and Escape Sequences Printing Literals – Most characters can be printed – Certain "problem" characters, such as the quotation mark ( " ) – Must be represented by escape sequences Represented by a backslash \ followed by an escape character

44  1992-2007 Pearson Education, Inc. All rights reserved. 44 Fig. 29.23 | Escape sequences.

45  1992-2007 Pearson Education, Inc. All rights reserved. 45 29.13 Formatting Output with Class Formatter Class Formatter – Provides same formatting capabilities as printf – Output formatted data to a specified destination E.g., a file on disk – By default, Formatter creates a string in memory String static method format – Create a string in memory without Formatter – Build a formatted string and assign the reference to a String reference variable

46  1992-2007 Pearson Education, Inc. All rights reserved. 46


Download ppt " 1992-2007 Pearson Education, Inc. All rights reserved. 1 29 Formatted Output."

Similar presentations


Ads by Google