Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Java Methods A & AB Chapter 10 - Strings

2 Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to format Strings Use the Character class to identify parts of Strings

3 Ch 10 Literal Strings – written in double quotes Ex) – String name = “Steven Austin”; Escape characters are interpreted ex) – String phrase = “Steve\n Austin”; The output is on two lines How do you output – “c:\AP\ch10.txt”?

4 Ch 10 Strings are immutable This means the value of a string can never change However a string object can reference a new value When this happens, java conveniently deletes the old value Ex) String name = “Bob”; String name = “Bobby”; //exists in different place in memory String name = “Bobby”; //exists in different place in memory

5 ch10 The String class has 9 constructors This means you can send it 9 different combinations of parameters String str = new String(); String str = new String( array, 4, 10); Above will print 10 characters from the array, starting with the 5 th value

6 Ch 10 We will typically just declare and initialize a String as follows: String str = “”; //empty String There are several methods in String class String name = “Mickey”; name.charAt(1); //returns char i name.concat(“ Mouse”); //returns Mickey Mouse name.startsWith(“Mick”); //boolean returns true Check out java doc for other methods What does name.length(); return?

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

8 Ch 10 Use Decimal Format class to convert doubles into Strings with specific formts Ex) DecimalFormat hundreths = new DecimalFormat(“0.00”); Double num = 35.2 String s1 = hundreths.format(num); s1 gives “35.20”

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

10 Ch 10 Extracting numbers from Strings String s = “43”; int n = Integer.parseInt(s); //uses Integer class This sometimes can cause an error, if the String isn’t a properly formatted number See page 275 to deal with this situation

11 Ch 10 Character methods Has convenient methods boolean methods isDigit, isLetter, isUpperCase, isLowerCase, isWhiteSpace, etc.. Can also convert Ex) toUpperCase method

12 Ch 10 StringBuffer class – character strings that can be modified Constructors StringBuffer() – empty string with capacity of 16 char StringBuffer(int n) – empty string with capacity of n char StringBuffer(String s) – constructs a string that holds a copy of s Check page 279 for some cool methods

13 Ch 10 Common methods length() – returns int of size of string capacity() – returns how big the buffer is

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

15 Ch 10 Exercise set #1,2,3,4a,5a,6,9,18,19,22 Show runs, no printing


Download ppt "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."

Similar presentations


Ads by Google