28 Formatted Output.

Slides:



Advertisements
Similar presentations
printf() Documentation info:
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
JavaScript, Third Edition
Introduction to C Programming
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CIS 270—Application Development II Chapter 28—Formatted Output.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Formatting Output. Formatting features of printf System.out.printf can perform the following Rounding floating-point values Aligning properly a column.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Classes and Objects
Chapter 9 C Formatted Input/Output
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Introduction to Scripting
Chapter 2 part #3 C++ Input / Output
OUTPUT STATEMENTS GC 201.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatting Output.
System.out.println for console output
IDENTIFIERS CSC 111.
Lecture 13 Input/Output Files.
INPUT & OUTPUT scanf & printf.
Chapter 9 - Formatted Input/Output
Introduction to Primitive Data types
Department of Computer and Information Science
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 part #3 C++ Input / Output
Chapter 2 Variables.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to Primitive Data types
Presentation transcript:

28 Formatted Output

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.

28.1   Introduction 28.2   Streams 28.3   Formatting Output with printf 28.4   Printing Integers 28.5   Printing Floating-Point Numbers 28.6   Printing Strings and Characters 28.7   Printing Dates and Times 28.8   Other Conversion Characters 28.9   Printing with Field Widths and Precisions 28.10   Using Flags in the printf Format String 28.11   Printing with Argument Indices 28.12   Printing Literals and Escape Sequences 28.13   Formatting Output with Class Formatter 28.14   Wrap-Up

28.1 Introduction Method printf Class Formatter 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

28.2 Streams Streams Sequences of bytes Can often be redirected Standard input – keyboard Standard output – screen Standard error – screen More in Chapters 14 and 24

28.3 Formatting Output with 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

28.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

28.4 Printing Integers Integer Format Whole number (no decimal point): 25, 0, -9 Positive, negative, or zero Only minus sign prints by default (later we shall change this) Format printf( format-string, argument-list ); format-string Describe the output format argument-list Contain the value corresponding to each format specifier

Fig. 28.1 | Integer conversion characters.

Outline Output positive and negative integers IntegerConversionT est.java Lines 8-10 Line 11 Line 12 Line 13 Program output Output positive and negative integers Output the integer in octal format Output the integer in hexadecimal format Output the integer in hexadecimal format with capital letters

28.5 Printing 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 107

Fig. 28.3 | Floating-point conversion characters.

Outline FloatingNumberTest .java Lines 8-10 Line 11 Line 12 Lines 13-14 Program output Output positive and negative floating-point numbers using the e conversion character Output floating-point number with uppercase E preceding the exponent Output floating-point number using the f conversion character Output floating-point number using the g and G conversion character

28.6 Printing Strings and Characters Conversion character c and C Require char C displays the output in uppercase letters Conversion character s and S String Object Implicitly use object’s toString method S displays the output in uppercase letters

Common Programming Error 28.1 Using %c to print a string causes an IllegalFormatConversionException—a string cannot be converted to a character.

Outline Display character with conversion character c CharStringConversi on.java Line 12 Line 13 Lines 14-15 Line 16 Program output Display character with conversion character c Display string with conversion character s Display string with conversion characters s and S Display Integer object with conversion characters s

28.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

Fig. 28.6 | Date and time composition conversion suffix characters.

Fig. 28.7 | Date formatting conversion suffix characters.

Fig. 28.8 | Time formatting conversion suffix characters.

Outline DateTimeTest.java (1 of 2) Line 10 Lines 13-17 Lines 20-22 Obtain a Calendar with the current date and time Use the Calendar object in printf statements as the value to be formatted with conversion character t Use the optional argument index to indicate that all format specifiers in the format string use the first argument

Outline DateTimeTest.java (2 of 2) Program output

28.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

Common Programming Error 28.2 Trying to print a literal percent character using % rather than %% in the format string might cause a difficult-to-detect logic error. When % appears in a format string, it must be followed by a conversion character in the string. The single percent could accidentally be followed by a legitimate conversion character, thus causing a logic error.

Fig. 28.10 | Other conversion specifiers.

Outline Print the value of boolean values false and true OtherConversion.ja va Lines 9-10 Lines 11-12 Lines 13-14 Line 15 Lines 16-17 Program output Print the value of boolean values false and true Associate a String and a null object to %b and %B Print the string representations of the hash code values for strings “hello” and “Hello” Print null in uppercase letters Print the % character in a string and a platform-specific line separator

28.9 Printing with Field Widths and Precisions Size of field in which data is printed 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)

28.9 Printing with Field Widths and Precisions (Cont.) 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.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 );

Common Programming Error 28.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!

Outline Print positive numbers with field width FieldWidthTest.jav a Lines 8-12 Lines 14-18 Program output Print positive numbers with field width Print negative numbers with field width

Outline PrecisionTest.java Line 11 Line 14 Program output Print same floating-point number with same precision but different conversion character Print string with precision

28.10 Using Flags in the printf Format String Supplement formatting capabilities Place flag immediately to the right of percent sign Several flags may be combined

Fig. 28.14 | Format string flags.

Outline MinusFlagTest.java Line 10 Lines 11-12 Program output Right justify a string, an integer, a character and a floating-point number left justify a string, an integer, a character and a floating-point number

Outline Print a positive number with a plus sign PlusFlagTest.java Program output Print a positive number with a plus sign

Outline Prefix a space to the positive number with the space flag SpaceFlagTest.java Line 8 Program output Prefix a space to the positive number with the space flag

Outline PoundFlagTest.java Lines 10-11 Program output Use the # flag to prefix 0 to the octal value and 0x to the hexadecimal value

Outline ZeroFlagTest.java Line 8 Line 9 Line 10 Program output Combine the + flag and the 0 flag print 452 in a field of width 9 with a + sign and leading zeros Print 452 in a field of width 9 using only the 0 flag Print 452 in a field of width 9 using only the space flag

Outline CommaFlagTest.java Lines 8-10 Program output Use the comma flag to display a decimal and a floating-point number with the thousands separator

Outline Enclose negative numbers in parentheses using the ( flag ParenthesesFlagTes t.java Lines 8-10 Program output Enclose negative numbers in parentheses using the ( flag

28.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

Outline ArgumentIndexTest Lines 11-13 Program output Print arguments in the argument list in reverse order using the argument index

28.12 Printing Literals and Escape Sequences 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

Common Programming Error 28.4 Attempting to print as literal data in a printf statement a double quote or backslash character without preceding that character with a backslash to form a proper escape sequence might result in a syntax error.

Fig. 28.23 | Escape sequences.

28.13 Formatting Output with 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

Outline FormatterTest.java Line 11 Line 12 Line 15 Program output Create a Formatter object using the default constructor, which will build a string in memory Invoke method format to format the output Invoke Formatter’s toString method to get the formatted data as a string