 1992-2007 Pearson Education, Inc. All rights reserved. 1 29 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.
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CMT Programming Software Applications
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.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Formatting and Parsing. Slide 2 Introduction Two core topics Parsing - Converting strings to other types (numbers, dates, …) Formatting Converting those.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 2: Using Data.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
 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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 9 C Formatted Input/Output
TMF1414 Introduction to Programming
OUTPUT STATEMENTS GC 201.
Chapter 08- printf and scanf
Formatting Output.
Lecture 13 Input/Output Files.
Chapter 9 - Formatted Input/Output
Introduction to Java Applications
Chapter 3 Introduction to Classes, Objects Methods and Strings
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

 Pearson Education, Inc. All rights reserved Formatted Output

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

 Pearson Education, Inc. All rights reserved 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 Using Flags in the printf Format String Printing with Argument Indices Printing Literals and Escape Sequences Formatting Output with Class Formatter Wrap-Up

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved. 9 Fig | Integer conversion characters.

 Pearson Education, Inc. All rights reserved. 10

 Pearson Education, Inc. All rights reserved Printing Floating-Point Numbers Floating Point Numbers – Have a decimal point ( 33.5 ) – Computerized scientific notation (exponential notation) is x 10² in scientific is e+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

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

 Pearson Education, Inc. All rights reserved. 13

 Pearson Education, Inc. All rights reserved 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

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

 Pearson Education, Inc. All rights reserved. 16

 Pearson Education, Inc. All rights reserved 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

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

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

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

 Pearson Education, Inc. All rights reserved. 21

 Pearson Education, Inc. All rights reserved. 22

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved. 24 Fig | Other conversion specifiers.

 Pearson Education, Inc. All rights reserved. 25

 Pearson Education, Inc. All rights reserved 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 )

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved 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", );

 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!

 Pearson Education, Inc. All rights reserved. 30

 Pearson Education, Inc. All rights reserved. 31

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved. 33 Fig | Format string flags.

 Pearson Education, Inc. All rights reserved. 34

 Pearson Education, Inc. All rights reserved. 35

 Pearson Education, Inc. All rights reserved. 36

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

 Pearson Education, Inc. All rights reserved. 38

 Pearson Education, Inc. All rights reserved. 39

 Pearson Education, Inc. All rights reserved. 40

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved. 42

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved. 44 Fig | Escape sequences.

 Pearson Education, Inc. All rights reserved 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

 Pearson Education, Inc. All rights reserved. 46