05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30

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

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Programming is instructing a computer to perform a task for you with the help of a programming language.
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.
28/08/2015SJF L31 F21SF Software Engineering Foundations ASSUMPTIONS AND TESTING Monica Farrow EM G30 Material available on Vision.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
07/09/2015SJF L41 F21SF Software Engineering Foundations 4 More Programming concepts Using the Car class Constants, Math class, boolean, formatting SELECTION.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Lesson 1: Introduction to ABAP OBJECTS Todd A. Boyle, Ph.D. St. Francis Xavier University.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
ITP © Ron Poet Lecture 3 1 Comments. ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
02 Variables1November Variables CE : Fundamental Programming Techniques.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
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.
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.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
CSC 142 H 1 CSC 142 Standard input/output. CSC 142 H 2 Console Ouput  Use the static PrintStream object out in System System.out.println("Hello, world");
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Input, Output and Variables GCSE Computer Science – Python.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
CS 106A, Lecture 4 Introduction to Java
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Formatting Output.
Introduction to Programming
Variables, Expressions, and IO
OUTPUT STATEMENTS GC 201.
Introduction to Programming
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Differences between Java and C
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
The keyboard is the standard input device.
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Output Manipulation.
Java: Variables, Input and Arrays
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30 Material available on Vision Dept of Computer Science

05/09/2015SJF L32 Formatting output From java 1.5 onwards, there are formatting features similar to C, which look complicated. You specify the type and the spacing. This lecture covers an absolute minimum You’re welcome to find out and use other formatting functionality If you need further information, see berformat.html berformat.html matting.html matting.html

05/09/2015SJF L33 Formatting output There is a static format method within the String class. Call using the class name, like a Math function The parameters are formatting instructions and the item to be formatted. A formatted String is returned You usually need to format a real number, to limit the number of decimal places displayed. If you are concatenating variables within a sentence, this is the only formatting that you need to do. However, if you want a table, you can use the width component to fix numbers and text to a fixed width.

05/09/2015SJF L34 Formatting real numbers In the String format method, the value of the 2 nd parameter is returned using the format specified in the 1 st parameter String myDistString = String.format("%.1f", distance); If distance contained a double of recurring, myDistString = “296.7” "%.1f" means format a real number to 1 decimal place % means the string contains formatting information.1 is the number of decimal places after the. f means it is a real number

05/09/2015SJF L35 Fixed width decimal numbers "%-6.2f" % means the string contains formatting information - left aligned, default is right aligned 6 is the minimum width, output will be padded with spaces.2 is the number of decimal places after the. f means it is a real number E.g. String.format(“%6.2f”, ) => “ 2.33” E.g. String.format(“%-6.2f”, ) => “2.33 ” To print a number within a sentence, omit the minimum width component

05/09/2015SJF L36 Fixed width integers "%-6d" % means the string contains formatting information - left aligned, default is right aligned 6 is the minimum width, output will be padded with spaces d means it is an integer number E.g. String.format(“%6d”, 123) => “ 123” E.g. String.format(“%-6d”, 123) => “123 ”

05/09/2015SJF L37 Fixed-width text Use this parameter in the String.format method: %-10s - left aligned, default is right aligned 10 is the minimum width, output will be padded with spaces s means text The following method call returns "Hello " String.format("%-8s", "Hello"); The following method call returns " Hello" String.format("%8s", "Hello");

05/09/2015SJF L38 Width known at runtime You can also create the formatting parameter at run time. E.g. int width = 8; String s = String.format("%"+ width + "s","Hello"); We could use this approach to centre text within a String (same number of blanks on each side). To start with, we need to divide the total number of blanks by 2, to find out how many on each side. Use integer division (there might be a remainder)

05/09/2015SJF L39 Converting number to String Why? Usually, to print or display You can convert a number to text using the formatting commands String.format You can also use the String.valueOf command but this will not format it int num = 5; String numText = String.valueOf(num); If you concatenate a number with a String, the result is a String 5 + “A” => “5A” 5 + “” => “5”

05/09/2015SJF L310 Converting a String to a number Why? Usually, if read from a text file or from user input, to use in calculations. From L11 on. The Integer class has a method to do this. String idtext = “1234”; int id = Integer.parseInt(idtext); If the text is non-numeric (e.g. “ABCD”), you will get a number format exception Covered in L09 on exceptions