String Formatting Preparing strings for output. Printing println( arg ) prints its one argument on a line println can be used to print any single value,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
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.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
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.
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.
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.
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.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
Formatting and Parsing. Slide 2 Introduction Two core topics Parsing - Converting strings to other types (numbers, dates, …) Formatting Converting those.
Chapter 2 – Continued Basic Elements of Java. Chapter Objectives Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings Commonly.
Chapter 2: Java Fundamentals Type conversion,String.
Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
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.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
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 first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
5-1 Embedded Systems C Programming Language Review and Dissection III Lecture 5.
Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
ITM © Port, KazmanVariables - 1 ITM 352 Data types, Variables Class #4.
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.
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.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
ITM 352 Data types, Variables
TMF1414 Introduction to Programming
Chapter 3: I/O Management
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.
System.out.println for console output
C Formatted Input / Output Review and Lab Assignments
Chapter 9 - Formatted Input/Output
Recap Week 2 and 3.
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
CS 1054 Introduction to Programming in Java
Presentation transcript:

String Formatting Preparing strings for output

Printing println( arg ) prints its one argument on a line println can be used to print any single value, but it doesn’t work well with arrays or user-defined data types scala> println(List(1, 2, 3)) List(1, 2, 3) scala> println(Array(1, 2, 3)) When printing a user-defined type, println will first look to see if you have written a toString method that it can use You can use println without an argument to get a blank line print( arg ) works like println, but doesn’t go to the next line scala> print("abc"); print("xyz") abcxyz 2

Concatenation The simplest way to print several values at once is to “add” (concatenate) them with a string, with the + operator When a value is concatenated with a string, the value’s toString method is called scala> println("Array is " + Array(1, 2, 3) + ", List is " + List(1, 2, 3)) Array is List is List(1, 2, 3) When you study classes, you will learn how to write your own toString methods 3

Interpolation You can insert (interpolate) the value of simple variables into strings by doing two things: Prefix the string with an s Put a $ before the variable name Example: s"Your score is $score" You can also interpolate the value of expressions by enclosing them in curly braces after the $ Example: s"Twice $x is ${2 * x}" 4

Formatting I Sometimes you want more precise control over the way numbers and dates are printed Default: scala> println(7 * 0.1) scala> println(new java.util.Date) Sat Jul 20 12:59:33 PDT 2013 Formatted: scala> println("Seven tenths is %5.3f".format(7 * 0.1)) Seven tenths is scala> println("Today's date is %tD".format(new java.util.Date)) Today's date is 07/20/13 5

Formatting The syntax is: string.format( value, …, value ) The number and types of values must agree with the format specifiers Here are examples of the most common format specifiers %12d format an integer right-justified in a field of width 12 %-8d format an integer left-justified in a field of width 8 %5.3f format a floating point number right-justified in a field of width 5, with 3 digits after the decimal point %12.3e like %12.3f, but using scientific ( E ) notation %7b format a boolean right-justified in a field of width 7 %10s format a string right-justified in a field of width 10 If a field width is too small, will be ignored Irregular spacing is better than printing the wrong number! 6

printf printf( string, value, …, value ) is shorthand for print( string.format( value, …, value )) Note that this is a shorter way to call print, not println To do the equivalent of a println, put a \n as the last character in the string 7

8 The End