Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to...

15
Java Methods A & Java Methods A & AB AB Chapter 10 - Strings Chapter 10 - Strings

Transcript of Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to...

Page 1: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Java Methods A & ABJava Methods A & AB

Chapter 10 - StringsChapter 10 - Strings

Page 2: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

GoalsGoals

Understand Strings indepthUnderstand Strings indepth

Learn strategies to deal with the Learn strategies to deal with the immutability of Stringsimmutability of Strings

Learn how to format StringsLearn how to format Strings

Use the Character class to identify parts of Use the Character class to identify parts of StringsStrings

Page 3: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Literal Strings – written in double quotesLiteral Strings – written in double quotes

Ex) – String name = “Steven Austin”;Ex) – String name = “Steven Austin”;

Escape characters are interpretedEscape characters are interpreted

ex) – String phrase = “Steve\n Austin”;ex) – String phrase = “Steve\n Austin”;

The output is on two linesThe output is on two lines

How do you output – “c:\AP\ch10.txt”?How do you output – “c:\AP\ch10.txt”?

Page 4: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Strings are immutableStrings are immutableThis means the value of a string can never This means the value of a string can never changechangeHowever a string object can reference a new However a string object can reference a new valuevalueWhen this happens, java conveniently deletes When this happens, java conveniently deletes the old valuethe old valueEx) String name = “Bob”;Ex) String name = “Bob”; String name = “Bobby”; //exists in different place in String name = “Bobby”; //exists in different place in

memorymemory

Page 5: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

ch10ch10

The String class has 9 constructorsThe String class has 9 constructors

This means you can send it 9 different This means you can send it 9 different combinations of parameterscombinations of parameters

String str = new String();String str = new String();

String str = new String( array, 4, 10);String str = new String( array, 4, 10);

Above will print 10 characters from the Above will print 10 characters from the array, starting with the 5array, starting with the 5thth value value

Page 6: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

We will typically just declare and initialize a We will typically just declare and initialize a String as follows:String as follows:String str = “”; //empty StringString str = “”; //empty StringThere are several methods in String classThere are several methods in String classString name = “Mickey”;String name = “Mickey”;name.charAt(1); //returns char iname.charAt(1); //returns char iname.concat(“ Mouse”); //returns Mickey Mousename.concat(“ Mouse”); //returns Mickey Mousename.startsWith(“Mick”); //boolean returns truename.startsWith(“Mick”); //boolean returns trueCheck out java doc for other methodsCheck out java doc for other methodsWhat does name.length(); return?What does name.length(); return?

Page 7: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Convert a number into a string:Convert a number into a string:Just concatenate a stringJust concatenate a stringint n = 45;int n = 45;String s = “” + n; // now n=“45”String s = “” + n; // now n=“45”You can also use a String methodYou can also use a String methodString s = Integer.toString(n);String s = Integer.toString(n);This is called Wrapping – converting a This is called Wrapping – converting a primitive data type into an objectprimitive data type into an object

Page 8: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Use Decimal Format class to convert Use Decimal Format class to convert doubles into Strings with specific formtsdoubles into Strings with specific formts

Ex) DecimalFormat hundreths = new Ex) DecimalFormat hundreths = new DecimalFormat(“0.00”);DecimalFormat(“0.00”);

Double num = 35.2Double num = 35.2

String s1 = hundreths.format(num);String s1 = hundreths.format(num);

s1 gives “35.20”s1 gives “35.20”

Page 9: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

PrintfPrintfOutputs a formatted StringOutputs a formatted String%f – double – can do field width and decimal places%f – double – can do field width and decimal places%d – int%d – int%c – char, %s - String%c – char, %s - StringDouble pct = 55.55;Double pct = 55.55;Int amt = 103;Int amt = 103;String title = “Percent and Amount”;String title = “Percent and Amount”;What does the following print?What does the following print?System.out.print(“%5.3f,%2d,%c”, pct,amt,title);System.out.print(“%5.3f,%2d,%c”, pct,amt,title);

Page 10: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Extracting numbers from StringsExtracting numbers from Strings

String s = “43”;String s = “43”;

int n = Integer.parseInt(s); //uses Integer int n = Integer.parseInt(s); //uses Integer classclass

This sometimes can cause an error, if the This sometimes can cause an error, if the String isn’t a properly formatted numberString isn’t a properly formatted number

See page 275 to deal with this situationSee page 275 to deal with this situation

Page 11: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Character methodsCharacter methods

Has convenient methods boolean methodsHas convenient methods boolean methods

isDigit, isLetter, isUpperCase, isDigit, isLetter, isUpperCase, isLowerCase, isWhiteSpace, etc..isLowerCase, isWhiteSpace, etc..

Can also convertCan also convert

Ex) toUpperCase methodEx) toUpperCase method

Page 12: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

StringBuffer class – character strings that can be StringBuffer class – character strings that can be modifiedmodifiedConstructorsConstructorsStringBuffer() – empty string with capacity of 16 StringBuffer() – empty string with capacity of 16 charcharStringBuffer(int n) – empty string with capacity of StringBuffer(int n) – empty string with capacity of n charn charStringBuffer(String s) – constructs a string that StringBuffer(String s) – constructs a string that holds a copy of sholds a copy of sCheck page 279 for some cool methodsCheck page 279 for some cool methods

Page 13: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Common methodsCommon methods

length() – returns int of size of stringlength() – returns int of size of string

capacity() – returns how big the buffer iscapacity() – returns how big the buffer is

Page 14: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

ch10ch10

Example)Example)StringBuffer sb = new StringBuffer(9);StringBuffer sb = new StringBuffer(9);sb.append(“we”);sb.append(“we”);Sb.insert(0,’a’);Sb.insert(0,’a’);Sb.insert(3,’ss’);Sb.insert(3,’ss’);sb.setCharAt(4,’o’);sb.setCharAt(4,’o’);Sb.append(“met”);Sb.append(“met”);Sb.deleteCharAt(8);Sb.deleteCharAt(8);String str = sb.toString();String str = sb.toString();What does str hold?What does str hold?

Page 15: Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.

Ch 10Ch 10

Exercise set #1,2,3,4a,5a,6,9,18,19,22Exercise set #1,2,3,4a,5a,6,9,18,19,22

Show runs, no printingShow runs, no printing