chapter 2: java fundamentals type conversion,string

30
Chapter 2: Java Fundamentals Type conversion ,String

Upload: lorena-anthony

Post on 03-Jan-2016

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 2: Java Fundamentals Type conversion,String

Chapter 2: Java Fundamentals

Type conversion ,String

Page 2: Chapter 2: Java Fundamentals Type conversion,String

Chapter Objectives

Type ConversionString ClassCommonly Used String MethodsParsing Numeric StringsCommonly Used Escape Sequences

Page 3: Chapter 2: Java Fundamentals Type conversion,String

Variables – Cont.

FloatThe default type of floating point numbers is double .The declaration :

float rate = 15.5f ; without the f , the compiler will generate an error

3

Page 4: Chapter 2: Java Fundamentals Type conversion,String

Variables – Cont.

CharWhen using the char data type, you enclose each character

represented within single quotations marks.Ex:

char c = ‘A’

char x = ‘&’

char space = ‘ ‘Each character is represented by a value (‘A’ is represented by the

value 65)

4

Page 5: Chapter 2: Java Fundamentals Type conversion,String

5

Page 6: Chapter 2: Java Fundamentals Type conversion,String

Type Conversion (Casting)

Used:• to change one data type to another .

• to avoid implicit type coercion.

Syntax:

(dataTypeName) expression

Expression evaluated first, then the value is converted to dataTypeName

Java Programming: From Problem Analysis to Program Design, Second Edition

6

Page 7: Chapter 2: Java Fundamentals Type conversion,String

Type Conversion (Casting)

Examples:1. (int)(7.9 + 6.7) = 142. (int)(7.9) + (int)(6.7) = 133. (double)(17) = 17.04. (double)(8+3) = (double)11 = 11.05. (double)(7) /2 = 7.0/2 = 3.56. (double)(7/2) = 3.07. (int)(7.8+(double)(15)/2) =

(int)15.3 =15

Java Programming: From Problem Analysis to Program Design, Second Edition

7

Page 8: Chapter 2: Java Fundamentals Type conversion,String

Type Conversion (Casting)

8. (int)(‘A’) = 65 9. (int)(‘8’) = 5610. (char)(65) = ‘A’ 11. (char)(56) = ‘8’

Java Programming: From Problem Analysis to Program Design, Second Edition

8

Page 9: Chapter 2: Java Fundamentals Type conversion,String

The class String

Contains operations to manipulate strings.String:

Sequence of zero or more characters.Enclosed in double quotation marks.Is processed as a single unit .Null or empty strings have no characters. “ “Every character has a relative position , the first character is in

position 0 .

Java Programming: From Problem Analysis to Program Design, Second Edition

9

Page 10: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 10

The class String

Java system automatically makes the class String available (i.e no need to import this class )

Example :

Consider the following declaration :

String sentence ;

sentence = “programming with java”

Page 11: Chapter 2: Java Fundamentals Type conversion,String

The class String

Length of the string is the number of characters in it .

When determining the length of a string , blanks count .

Example :“ “ has length = 0 “abc” has length = 3 , position of a = 0 ,b= 1 , c= 2“a boy” has length = 5

Java Programming: From Problem Analysis to Program Design, Second Edition

11

Page 12: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 12

Some Commonly Used String Methods

Page 13: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 13

Some Commonly Used String Methods

Page 14: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 14

Some Commonly Used String Methods

Page 15: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 15

Some Commonly Used String Methods

Page 16: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 16

Examples on string methods

String s1 , s2 , s3 ;

s1 = “abcdefeg” ;

System.out.println( s1.length() ); // 8

System.out.println(s1.charAt(3)); //d

System.out.println(s1.indexOf(‘e’)); //4

System.out.println(s1.indexOf(“cd”)); //2

System.out.println(s1.toUpperCase()); //ABCDEFEG

Page 17: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 17

Examples on string methods

System.out.println(s1.substring(1 , 4)); //bcdSystem.out.println(s1 + “xyz”); // abcdefegxyzSystem.out.println( s1.replace(‘d’ ,’D’)); // abcDefegSystem.out.println(s1.charAt(4) ); // eSystem.out.println(s1.indexOf(‘b’)); // 1System.out.println(s1.indexOf(‘e’,5)); // 6

Page 18: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 18

Input

Reading a Single Character

if ch is a char variable. To input A into ch, you can use the following statement:

ch = console.next().charAt(0);

Page 19: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 19

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.

Page 20: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 20

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") = 6723Integer.parseInt("-823") = -823

Page 21: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 21

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") = 34.56Float.parseFloat("-542.97") = -542.97

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") = 345.78Double.parseDouble("-782.873") = -782.873

Page 22: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 22

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 and argumentList is a list of arguments.

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.

Page 23: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 23

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.

Page 24: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 24

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 =59.05511811023The o/p would be :

There are 59.06 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.

Page 25: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 25

Example1public class Example3_6{ public static void main (String[] args) { int num = 763; double x = 658.75; String str = "Java Program."; System.out.println("123456789012345678901234567890");

System.out.printf ( "%5d%7.2f%15s%n", num, x, str); System.out.printf ("%15s%6d%9.2f %n", str, num, x); } }

Page 26: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 26

Example1

Sample run :123456789012345678901234567890

763 658.75 Java Program. Java Program. 763 658.75

Page 27: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 27

Example2public class Example3_7{ public static void main (String[] args) { int num = 763;

double x = 658.75; String str = "Java Program."; System.out.println("123456789012345678901234567890"); System.out.printf("%-5d%-7.2f%-15s ***%n", num, x, str);

System.out.printf("%-15s%-6d%- 9.2f ***%n", str, num, x); }}

Page 28: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 28

Example2Sample Run : 123456789012345678901234567890

763 658.75 Java Program. ***Java Program. 763 658.75 ***

Page 29: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 29

Formatting Output with printf

Page 30: Chapter 2: Java Fundamentals Type conversion,String

Java Programming: From Problem Analysis to Program Design, Second Edition 30

Commonly Used Escape Sequences