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

Nested Loops. Nested loops Just as a selection structure can be nested within another selection structure (or within a loop), a loop can also be nested.
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.
© 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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Computer Programming Lab(4).
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.
 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.
Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
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.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright © Curt Hill Mathematics Functions An example of static methods.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
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
Boolean Expressions and Conditionals (If Statements)
Formatted Output (printf)
Exceptions and User Input Validation
First Programs CSE 1310 – Introduction to Computers and Programming
OUTPUT STATEMENTS GC 201.
Common Mistakes with Functions
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
Chapter 9 - Formatted Input/Output
Introduction to Computer Programming
Variables, Types, Operations on Numbers
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)
Computer Science Club 1st November 2019.
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. 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. – %b is for values of type boolean. 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 10 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". Note the words “at least” in the above explanations. – If you specify a certain width, but the value actually needs more width than that in order to be displayed, it will be given the width that it is needed. For example, if you use %10s, but the string has 15 characters, then all 15 characters will be printed. 10

Specifying Width By specifying a width for every value, you get nicely aligned columns in the output. 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", ); }

Specifying Width 12 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 13 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 14 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", ); } When you want to print a new line, put the special code \n in your text.

Printing a New Line with \n 15 public class example1 { public static void main(String[] args) { System.out.printf("%20s, 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. 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? 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 t i in the line above? – t 1 = "There are " – t 2 = " days in " – t 3 = "\n" 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? 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 f i in the line above? – f 1 = %d – f 2 = %s 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? 21

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" 22

The Circles Program, Revisited 23 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 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: 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 25 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

Example: Computing Squares Write a program that: – Asks the user to enter a number. – Gets the number from user input. – Prints: The square of X is Y where X is the number that the user typed, and Y is the square of X. – Prints only two decimal digits. 26

Example: Computing Squares 27 import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter a number: "); double number = in.nextDouble(); double square = Math.pow(number, 2); System.out.printf("The square of %.2f is %.2f\n", number, square); } Example Output: Enter a number: 5 The square of 5.00 is Example Output: Enter a number: 2.4 The square of 2.40 is 5.76