The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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(,,,..., );
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 5 Loops Section 1 - While Loops Section 2 - For Loops
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 5 Loops, Printf Statements, Nested Control Statements and GUI Input & Output Section 1 - While Loops Section 2 - Printf Statements Section 3 -
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Formatting Screen Output How do I make my numbers look pretty?
 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.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Asking the USER for values to use in a software 1 Input.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
© 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?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
28 Formatted Output.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Formatting Screen Output How do I make my numbers look pretty?
Chapter 2 Screen Output Section 2.1
OUTPUT STATEMENTS GC 201.
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
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
Data Types and Expressions
CS 1054 Introduction to Programming in Java
Presentation transcript:

The Print Formatting Statement … named printf

2 Introduction to printf statements print and println statements don’t allow us to easily format output to the screen or a file. However, a printf statement does. Just like the ln in println is an abbreviation for line. The f in printf is an abbreviation for format. printf statements allow us to left justify output or right justify output, separately or we can use both in the same statement to do things like align output in columns. This makes the output more readable to the user of a program. There are a number of things to understand before we start using them, so let’s take our time and understand all the different parts of a printf statement so we will know how to write them correctly.

3 The Form of a printf Statement A printf statement always has the following form: System.out.printf ("format string", dataValue1, dataValue2,....); Notice the format string and all data values are separated by commas NOT + signs. The format string is inside double quotes and there must be a format specifier in it for each data value listed in the remainder of the statement. You can have any number of data values, but you must have one format specifier for every data value. The data values are NOT within double quotes. Format specifiers always begin with the % symbol.

4 The printf Format Specifiers There are three format specifiers that we will use: the letter d for int values as in %5d the letter f for floating point values as in %8.2f the letter s for string values as in %10s Also %n tells Java to display a new-line character. You can also use the escape sequence \n. If a dash - follows the % symbol of a format specifier, then the data value will be left justified. If there is no dash then the data value is right justified. The - is called a format flag.

5 The Format Specifier for int printf statements can right justify integer output. Consider this code: int fahrenheit = 212; System.out.printf("%10d”, fahrenheit); Once java sees the format specifier %10d then it retrieves the int data value stored in the variable fahrenheit (212). Java figures out that it needs 3 spaces to print 212 and then skips 7 spaces from the left margin and then begins printing 212. It prints 212 in the 8th, 9th, and 10th spaces of the field of 10 spaces. If we use a dash character ‘-’ to represent a blank space, then the output would look like the following: The printf statement right justifies the value stored in the variable fahrenheit in a field width of 10 spaces.

6 Right Justified Output for int Here are random integers between 1 and 2000 that are right justified using a prinf statement with a field width of 6. Notice the ones digits,tens digits, hundreds digits, and thousands digits are lined up

7 Left Justification for int You can use a printf statement to left justify output also. Consider the code: int fahrenheit = 212; System.out.printf("%-10d”, fahrenheit); Once java sees the format specifier %-10d then it retrieves the data value stored in the variable fahrenheit (212). Java figures out that it needs 3 spaces to print 212 and then begins printing 212 at the left margin. It then skips 7 spaces. It prints 212 in the 1st, 2nd, and 3rd spaces of the field of 10 spaces. The output would look like the following: The printf statement uses the - format flag to left justify the value stored in the variable fahrenheit in a field width of 10 spaces.

8 The Format Specifier for double printf statements can be set to round what is printed out. They don’t actually round what is in the variable, but they round what is seen on the screen. In this example, we will print right justified a floating point value in a field width of 8 with a precision of 4. double percentage = ; System.out.printf("%10.4f”, percentage ); Once java sees the format specifier %10.4f then it retrieves the data value stored in the variable percentage ( ). Java figures out that it needs 4 spaces to print the part of the number to the right of the decimal point. It uses the remaining spaces: = 6 to print the decimal point and the part of the number to the left of the decimal. Note how the number is rounded when it is printed. The output would look like the following:

9 Left Justification for double We can use a printf statement to round but use left justification instead. In this example, we will print left justified a floating point value in a field width of 8 with a precision of 4. double percentage = ; System.out.printf("%-10.4f”, percentage ); Note how the number is rounded and left justified when it is printed. Any other output will start after the 3 spaces. The output would look like the following:

10 The Format Specifier for string printf statements can be used to display string information in quite a few number of ways. In this example, we will print right justified a string value in a field width of 20. String phrase = “Java is Cool!”; System.out.printf("%20s”, phrase); Once java sees the format specifier %20s then it retrieves the data value stored in the variable phrase(“Java is Cool!”). Java figures out that it needs 13 spaces to print the phrase. It uses the remaining spaces: = 7 to print 7 spaces before the phrase. The output would look like the following: Java is Cool!

11 Left Justification for string In this example, we will print left justified a string value in a field width of 20. String phrase = “Java is Cool!”; System.out.printf("%-20s”, phrase); Once java sees the format specifier %-20s then it retrieves the data value stored in the variable phrase(“Java is Cool!”). Java figures out that it needs 13 spaces to print the phrase. It uses the remaining spaces: = 7 to print 7 spaces after the phrase. Any other output has to begin after those 7 spaces. The output would look like the following: Java is Cool!

12 The New Line Format Specifier None of the previous examples start a new line after printing the data values. To start a new line after printing the data value, the new line format specifier %n must be included in the printf statement. All of the lines below will print the data values and start a new line. System.out.printf("%10d%n”, fahrenheit); System.out.printf("%-10d%n”, fahrenheit); System.out.printf("%10.4f%n”, percentage ); System.out.printf("%-10.4f%n”, percentage ); System.out.printf("%20s%n”, phrase); System.out.printf("%-20s%n”, phrase); Note: when Java sees the %n format specifier, it doesn’t look for a data value like it does with other format specifiers.

13 Multiple Format Specifiers in One Line All of the previous printf statements have only one data value that is formatted whether a new line is created or not. Numerous format specifiers and data values can be included in one printf statement. All of the format specifiers must appear in the format string. System.out.printf("%10d%10.4f%20s%n”, fahrenheit, percentage, phrase); The output all on one line would look like this: Java is Cool!

14 Multiple Format Specifiers in One Line Sometimes we want to have numerous format specifiers and data values in one printf statement, but we want to also print out additional string information. Here is an example of how you might do that: for (???”) { double square = ???; double cube = ???; System.out.printf("The square of %5.1f is %6.1f and the cube is %7.1f%n", i, square, cube); } The output all on one line would look something like this: The square of 1.0 is 1.0 and the cube is 1.0 The square of 2.0 is 4.0 and the cube is 8.0 The square of 3.0 is 9.0 and the cube is 27.0 The square of 4.0 is 16.0 and the cube is 64.0 The square of 5.0 is 25.0 and the cube is 125.0

Ch 8 Sect 4 The String.format Statement Sometimes we may want to format text but not print it out. We can do this by using String.format and storing the formatted string in a string variable. Look at how we substitute String.format for System.out.prinf and store the formatted string in a String variable. double dollars = 25; double tax = dollars * 0.125; String s1 = String.format(“Income: $%.2f%n”, dollars); String s2 = String.format(“Tax owed: $%.2f%n”, tax); Why would we do this? For example, we might want to format columns in a JTextArea in a GUI program. When we send the string to the JTextArea, we want it to be nicely formatted. We would only use System.out.printf if we were going to display things in the console window.