Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
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.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
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(,,,..., );
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
2.2 Information on Program Appearance and Printing.
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.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
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.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 2 Elementary Programming
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming
Chapter 9 - Formatted Input/Output
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Formatted Output (printf)
Exceptions and User Input Validation
TMF1414 Introduction to Programming
First Programs CSE 1310 – Introduction to Computers and Programming
OUTPUT STATEMENTS GC 201.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 9 - Formatted Input/Output
Introduction to Computer Programming
Variables, Types, Operations on Numbers
elementary programming
Introduction to Java Applications
Strings CSE 1310 – Introduction to Computers and Programming
First Programs CSE 1310 – Introduction to Computers and Programming
CS 1054 Introduction to Programming in Java
Input and Formatted Output (printf)
Presentation transcript:

Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

System.out.printf System.out.printf gives you an easy way to print nicer output, by combining text, variables, and other values. 2 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } Output: There are 31 days in July Average temperature in July: degrees

System.out.printf printf works as follows: – It starts printing the text in the first argument. 3 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } There are

System.out.printf printf works as follows: – It starts printing the text in the first argument. – When it finds the first % sign, it prints the second argument. 4 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } There are 31

System.out.printf printf works as follows: – It starts printing the text in the first argument. – When it finds the first % sign, it prints the second argument. – It continues printing text. 5 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } There are 31 days in

System.out.printf printf works as follows: – It starts printing the text in the first argument. – When it finds the first % sign, it prints the second argument. – It continues printing text. – When it finds the second % sign, it prints the third argument. 6 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); } There are 31 days in July

System.out.printf printf works as follows: – It starts printing the text in the first argument. – When it finds the first % sign, it prints the second argument. – It continues printing text. – When it finds the second % sign, it prints the third argument. – And so on, until the entire text is processed. 7 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; double temperature = 85.3; System.out.printf("There are %d days in %s\n", days, month); System.out.printf("Average temperature in %s: %f degrees\n", month, temperature); }

System.out.printf The values that you provide in the second argument, third argument, and so on, can be: – variables, like days in the example above. – constants, like "July" in the example above. – expressions, like: ( ) / 2.0 in the example above. Math.PI, (int)Math.floor(5.3) 8 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; System.out.printf("There are %d days in %s\n", days, "July"); System.out.printf("Average temperature in %s: %f degrees\n", month, ( ) / 2.0); }

Format Specifiers %d, %f, %s are called format specifiers. A format specifier must match the value that will be printed. – %d is for values of type int – %f is for values of type double – %s is for values of type String or char – %c is for values of type char (a type we will see soon). – %b is for values of type boolean (a type we will see soon). 9 public class example1 { public static void main(String[] args) { int days = 31; String month = "July"; System.out.printf("There are %d days in %s\n", days, "July"); System.out.printf("Average temperature in %s: %f degrees\n", month, ( ) / 2.0); }

Specifying Width After the % sign, you can put a number, specifying the minimum width for that value. For example: – %5d means "allocate at least 5 spaces for that int". – %10s means "allocate at least 7 spaces for that string". – %7f means "allocate at least 7 spaces for that double". – %7.2f means "allocate at least 7 spaces for that double, but only two after the decimal point". – %.2f means "allocate as many spaces as needed for that double, but only two after the decimal point". This way you get nicely aligned columns in the output. 10 public class example1 { public static void main(String[] args) { System.out.printf("%20s, current temperature: %8.2f\n", "Dallas", ); System.out.printf("%20s, current temperature: %8.2f\n", "San Francisco", ); System.out.printf("%20s, current temperature: %8.2f\n", "surface of the sun", ); }

Specifying Width 11 public class example1 { public static void main(String[] args) { System.out.printf("%20s, current temperature: %8.2f\n", "Dallas", ); System.out.printf("%20s, current temperature: %8.2f\n", "San Francisco", ); System.out.printf("%20s, current temperature: %8.2f\n", "surface of the sun", ); } Output: Dallas, current temperature: San Francisco, current temperature: surface of the sun, current temperature:

Not Specifying Width 12 public class example1 { public static void main(String[] args) { System.out.printf("%s, current temperature: %f\n", "Dallas", ); System.out.printf("%s, current temperature: %f\n", "San Francisco", ); System.out.printf("%s, current temperature: %f\n", "surface of the sun", ); } Output: Dallas, current temperature: San Francisco, current temperature: surface of the sun, current temperature: Compare the previous output to this one. In this version of the code, we do not specify widths in printf. The output does not look as nice.

Printing a New Line with \n 13 public class example1 { public static void main(String[] args) { System.out.printf("%s, current temperature: %8.2f\n", "Dallas", ); System.out.printf("%s, current temperature: %8.2f\n", "San Francisco", ); System.out.printf("%s, current temperature: %8.2f\n", "surface of the sun", ); } Output: Dallas, current temperature: San Francisco, current temperature: surface of the sun, current temperature: When you want to print a new line, put the special code \n in your text.

Printing a New Line with \n 14 public class example1 { public static void main(String[] args) { System.out.printf("%s, current temperature: %8.2f", "Dallas", ); System.out.printf("%20s, current temperature: %8.2f", "San Francisco", ); System.out.printf("%20s, current temperature: %8.2f", "surface of the sun", ); } Output: Dallas, current temperature: San Francisco, current temperature: surface of the sun, current temperature: If you forget new lines, the output can look pretty ugly!

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: Value v i should be printed at that point. The type of value v i. How many characters should v i occupy. – v i is an int, double, or string. It can be a variable. It can be a constant, like 5, or 2.5, or "hello". It can be any expression that evaluates to an int, double, or string. 15

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: – v i is an int, double, or string. System.out.printf("There are %d days in %s\n", 31, "July"); What is each t i in the line above? 16

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: – v i is an int, double, or string. System.out.printf("There are %d days in %s\n", 31, "July"); What is each t i in the line above? – t 1 = "There are " – t 2 = " days in " – t 3 = "\n" 17

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: – v i is an int, double, or string. System.out.printf("There are %d days in %s\n", 31, "July"); What is each f i in the line above? 18

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: – v i is an int, double, or string. System.out.printf("There are %d days in %s\n", 31, "July"); What is each f i in the line above? – f 1 = %d – f 2 = %s 19

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: – v i is an int, double, or string. System.out.printf("There are %d days in %s\n", 31, "July"); What is each v i in the line above? 20

Syntax of System.out.printf Syntax: System.out.printf("t 1 f 1 t 2 f 2 t 3 f 3 …t n f n t n+1 ", v 1, v 2, v 3, …, v n ); – t i is text. You can put in there whatever you want. – f i is a format specifier. It specifies several things: – v i is an int, double, or string. System.out.printf("There are %d days in %s\n", 31, "July"); What is each v i in the line above? – v 1 = 31 – v 2 = "July" 21

Examples (added 9/9/15) 22 public class hello1 { public static void main(String[] args) { System.out.printf("%-10.2f\n", 18.0); // left aligned: - System.out.printf("%10.2f\n", 20.0); // right aligned System.out.printf("%10.2f", 10.2); // no text System.out.printf("\n"); // only \n System.out.printf("%10.2f%5d\n", 15.7,3); // no text and \n System.out.printf("%10.2f%d\n", 15.7,3); System.out.printf("%10.2f%d", 15.7,3); System.out.printf("\n%10.2f\n%5d\n", 11.3,8); } Example Output: Notice: %-10.2f

Escape sequences (added 9/9/15) 23 public class hello1 { public static void main(String[] args) { System.out.printf("some\ttext\nmore text"); System.out.printf("\roverwrite"); System.out.printf("\nover\b\b\bwrite"); System.out.println("\n123456\b\b\b\bXY"); System.out.printf("\n\'\"\\"); System.out.printf("\n"); } } Escape sequences: \n - new line \t - tab \r - carriage return (back to the beginning of the line) \b - backspace (back one character) \' - insert single quote in text \" - insert double quote in text \\ - insert backslash in text Output: sometext overwrite owrite 12XY '"\ To print the % sign, use %. E.g. System.out.printf("%");

The Circles Program, Revisited 24 import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } <-- Last version we saw. Used println. Example Output:

The Circles Program, Revisited 25 import java.util.Scanner; public class hello1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.println(circumference); System.out.println(area); } <-- Last version we saw. Used println. Example Output: Please enter the radius: The output does not look very nice. Too many decimals. No text. Can we get output like this? Please enter the radius: 10 The circumference is The area is

The Circles Program, Revisited 26 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter the radius: "); double radius = in.nextDouble(); double circumference = 2 * Math.PI * radius; double area = Math.PI * Math.pow(radius, 2); System.out.printf("The circumference is %.2f.\n", circumference); System.out.printf("The area is %.2f.\n", area); } Improved version, using printf. Example Output: Please enter the radius: 10 The circumference is The area is