Java Fundamentals 5. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.

Slides:



Advertisements
Similar presentations
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Advertisements

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
Chapter 3: Introduction to Objects and Input/Output.
Review Session Ryan Ly (slides by Jai Madhok)
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(,,,..., );
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
CIS 270—Application Development II Chapter 28—Formatted Output.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© 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.
 Pearson Education, Inc. All rights reserved Formatted Output.
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 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Review Session Jai Madhok
Formatting Screen Output How do I make my numbers look pretty?
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
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.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Lecture 2 Objectives Learn about objects and reference variables.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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,
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
InterestRate Create an InterestRate class and InterestRateViewer client class to do the following: A person is purchasing an item with their credit card.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Simple Console Output CS 21a. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 3: Introduction to Objects and Input/Output
Java Fundamentals 4.
Java Programming Lecture 2
Formatted Output (printf)
Objectives You should be able to describe: Interactive Keyboard Input
Chapter 4 Intermediate (part 1)
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Java Programming: From Problem Analysis to Program Design, 4e
תרגול 1: סביבת העבודה ומבוא ל-Java
OUTPUT STATEMENTS GC 201.
תרגול 1: סביבת העבודה ומבוא ל-Java
IDENTIFIERS CSC 111.
Chapter 3: Introduction to Objects and Input/Output
Chapter 2: Basic Elements of Java
תרגול 1: סביבת העבודה ומבוא ל-Java
Chapter 3: Introduction to Objects and Input/Output
Introduction to Java Applications
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
CS 1054 Introduction to Programming in Java
Input and Formatted Output (printf)
Presentation transcript:

Java Fundamentals 5

Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are classes designed to convert a numeric string into a number.  These classes are called wrapper classes.  parseInt is a method of the class Integer, which converts a numeric integer string into a value of the type int.  parseFloat is a method of the class Float and is used to convert a numeric decimal string into an equivalent value of the type float.  parseDouble is a method of the class Double, which is used to convert a numeric decimal string into an equivalent value of the type double.

Java Programming: From Problem Analysis to Program Design, Second Edition3 Parsing Numeric Strings  A string consisting of only integers or decimal numbers is called a numeric string.  To convert a string consisting of an integer to a value of the type int, we use the following expression: Integer.parseInt(strExpression) Example: Integer.parseInt("6723") = 6723 Integer.parseInt("-823") = -823

Java Programming: From Problem Analysis to Program Design, Second Edition4 Parsing Numeric Strings  To convert a string consisting of a decimal number to a value of the type float, we use the following expression: Float.parseFloat(strExpression) Example: Float.parseFloat("34.56") = Float.parseFloat(" ") =  To convert a string consisting of a decimal number to a value of the type double, we use the following expression: Double.parseDouble(strExpression) Example: Double.parseDouble("345.78") = Double.parseDouble(" ") =

Java Programming: From Problem Analysis to Program Design, Second Edition5 Formatting Output with printf The syntax to use the method printf to produce output on the standard output device is: System.out.printf(formatString); or System.out.printf(formatString,argumentList) ; formatString is a string specifying the format of the output. argumentList is a list of arguments that consists of constant values, variables, or expressions. If there is more than one argument in argumentList, the arguments are separated with commas.

Java Programming: From Problem Analysis to Program Design, Second Edition6 Formatting Output with printf System.out.printf("Hello there!"); Consists of only the format string and the statement: System.out.printf("There are %.2f inches in %d centimeters.%n", centimeters / 2.54, centimeters); Consists of both the format string and argumentList. %.2f and %d are called format specifiers. By default, there is a one-to-one correspondence between format specifiers and the arguments in argumentList. The first format specifier, %.2f, is matched with the first argument, which is the expression centimeters / 2.54.

Java Programming: From Problem Analysis to Program Design, Second Edition7 Formatting Output with printf The second format specifier, %d, is matched with the second argument, which is centimeters. The format specifier %n positions the insertion point at the beginning of the next line. If centimeters = 150  150/2.54 = The o/p would be : There are inches in 150 centimeters The output of a printf statement is right-justified by default. To force the output to be left-justified, negative column widths may be used.

Java Programming: From Problem Analysis to Program Design, Second Edition8 Example1 public class Example3_6 { public static void main (String[] args) { int num = 763; double x = ; String str = "Java Program."; System.out.println(" "); System.out.printf ( "%5d%7.2f%15s%n", num, x, str); System.out.printf ("%15s%6d%9.2f %n", str, num, x); }

Java Programming: From Problem Analysis to Program Design, Second Edition9 Example1 Sample run : Java Program. Java Program

Java Programming: From Problem Analysis to Program Design, Second Edition10 Example2 public class Example3_7 { public static void main (String[] args) { int num = 763; double x = ; String str = "Java Program."; System.out.println(" "); System.out.printf("%-5d%-7.2f%-15s ***%n", num, x, str); System.out.printf("%-15s%-6d%- 9.2f ***%n", str, num, x); }

Java Programming: From Problem Analysis to Program Design, Second Edition11 Example2 Sample Run : Java Program. *** Java Program ***

Java Programming: From Problem Analysis to Program Design, Second Edition12 Formatting Output with printf

Java Programming: From Problem Analysis to Program Design, Second Edition 13 Commonly Used Escape Sequences