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

Similar documents
First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

STUDENT LESSON A7 Simple I/O

Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Section 2.2 Your First Program in Java: Printing a Line of Text

Full file at

Variables, Types, Operations on Numbers

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

CC112 Structured Programming

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Section 2.2 Your First Program in Java: Printing a Line of Text

Topics. The Development Process

Getting started with Java

Strings, Strings and characters, String class methods. JAVA Standard Edition

Chapter 2: Data and Expressions

Classes and Objects Part 1

Introduction to Java Applications

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Chapter 2: Data and Expressions

Introduction to Computer Programming

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Formatted Output Pearson Education, Inc. All rights reserved.

Java Console Input/Output The Basics

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

4 WORKING WITH DATA TYPES AND OPERATIONS

First Java Program - Output to the Screen

CS 106 Introduction to Computer Science I

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

COMP 202 Java in one week

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

COMP String and Console I/O. Yi Hong May 18, 2015

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

COMP 110 Introduction to Programming. What did we discuss?

Midterm Examination (MTA)

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

FUNDAMENTAL DATA TYPES

CS111: PROGRAMMING LANGUAGE II

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Arithmetic and IO. 25 August 2017

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

5) (4 points) What is the value of the boolean variable equals after the following statement?

Java Basic Datatypees

CS 302: Introduction to Programming

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

Introduction to Computer Science I

Unit 4. Input/Output Functions

Object-Oriented Programming in Java

COMP 202. Java in one week

Introduction to Java Applications; Input/Output and Operators

AWK - PRETTY PRINTING

Full file at

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

More on variables and methods

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Strings and Arrays. Hendrik Speleers

Simple Java Reference

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Files & Exception Handling. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Inf1-OP. Creating Classes. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Date: Dr. Essam Halim

Class Foo. instance variables. instance methods. Class FooTester. main {

Primitive Data Types: Intro

Building Java Programs

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

2: Basics of Java Programming

What did we talk about last time? Examples switch statements

Lecture Set 2: Starting Java

CS2 Assignment A1S The Simple Shapes Package

Lecture Set 2: Starting Java

Basic Computation. Chapter 2

Object Oriented Programming. Java-Lecture 1

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

This is a copy of the notes used in the module CI01.

Using Classes and Objects. Chapter

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

The Command Shell, Libraries and Clients, Formatted Printing. Fundamentals of Computer Science

Chapter. Let's explore some other fundamental programming concepts

CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays

Chapter 5 Lab Methods

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Chapter 2: Using Data

COMP102: Test July, 2006

CS141 Programming Assignment #5

Console Input and Output

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Transcription:

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

Formatted Printing - References Java API: https://docs.oracle.com/javase/7/docs/api/java/util/formatter.html#syntax Oracle Java tutorial (brief): https://docs.oracle.com/javase/tutorial/java/data/numberformat.html Note that you can use System.out.printf( ) to control the format in which you print information. You can also build a string (without printing it) using 2

System.out.printf 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: 85.300000 degrees System.out.printf gives you an easy way to print nicer output, by combining text, variables, and other values. 3

System.out.printf 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); printf works as follows: It starts printing the text in the first argument. There are 4

System.out.printf 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); printf works as follows: It starts printing the text in the first argument. When it finds the first % sign, it prints the second argument. There are 31 5

System.out.printf 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); 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. There are 31 days in 6

System.out.printf 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); 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. There are 31 days in July 7

System.out.printf 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); 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. 8

System.out.printf 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, (85.1 + 85.5) / 2.0); 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: (85.1 + 85.5) / 2.0 in the example above. Math.PI, (int)math.floor(5.3) 9

Format Specifiers 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, (85.1 + 85.5) / 2.0); %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). 10

Specifying Width System.out.printf("%20s, current temperature: %8.2f%n", "Dallas", 106.7431); System.out.printf("%20s, current temperature: %8.2f%n", "San Francisco", 64.918262); System.out.printf("%20s, current temperature: %8.2f%n", "surface of the sun", 12000.0); 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". This way you get nicely aligned columns in the output. 11

Specifying Width System.out.printf("%20s, current temperature: %8.2f%n", "Dallas", 106.7431); System.out.printf("%20s, current temperature: %8.2f%n", "San Francisco", 64.918262); System.out.printf("%20s, current temperature: %8.2f%n", "surface of the sun", 12000.0); Output: Dallas, current temperature: 106.74 San Francisco, current temperature: 64.92 surface of the sun, current temperature: 12000.00 12

Not Specifying Width System.out.printf("%s, current temperature: %f%n", "Dallas", 106.7431); System.out.printf("%s, current temperature: %f%n", "San Francisco", 64.918262); System.out.printf("%s, current temperature: %f%n", "surface of the sun", 12000.0); Output: Dallas, current temperature: 106.743100 San Francisco, current temperature: 64.918262 surface of the sun, current temperature: 12000.000000 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. 13

Printing a New Line with %n System.out.printf("%s, current temperature: %8.2f%n", "Dallas", 106.7431); System.out.printf("%s, current temperature: %8.2f%n", "San Francisco", 64.918262); System.out.printf("%s, current temperature: %8.2f%n", "surface of the sun", 12000.0); Output: Dallas, current temperature: 106.743100 San Francisco, current temperature: 64.918262 surface of the sun, current temperature: 12000.000000 When you want to print a new line, put the special code %n in your text. 14

Printing a New Line with %n System.out.printf("%s, current temperature: %8.2f", "Dallas", 106.7431); System.out.printf("%20s, current temperature: %8.2f", "San Francisco", 64.918262); System.out.printf("%20s, current temperature: %8.2f", "surface of the sun", 12000.0); Output: Dallas, current temperature: 106.74 San Francisco, current temperature: 64.92 surface of the sun, current temperature: If you forget new lines, the output can look pretty ugly! 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: 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

Examples (added 9/9/15) public class hello1 { 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: 18.00 20.00 10.20 15.70 3 15.703 15.703 11.30 8 Notice the effect of %-10.2f for 18.00 23

Escape sequences (added 9/9/15) public class hello1 { 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 (note: use %n (platform appropriate), not \n ) \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: some text overwrite owrite 12XY '"\ To print the % sign, use %%. E.g. System.out.printf("%%"); 24

The Circles Program, Revisited import java.util.scanner; public class hello1 { Scanner in = new Scanner(System.in); <-- Last version we saw. Used println. Example Output: "); 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); 25

The Circles Program, Revisited import java.util.scanner; public class hello1 { Scanner in = new Scanner(System.in); <-- Last version we saw. Used println. Example Output: "); 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); Can we get output like this? Please enter the radius: 10 62.83185307179586 314.1592653589793 The output does not look very nice. Too many decimals. No text. Please enter the radius: 10 The circumference is 62.83. The area is 314.16. 26

The Circles Program, Revisited import java.util.scanner; 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 62.83. The area is 314.16. 27