Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.

Similar presentations


Presentation on theme: "An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting."— Presentation transcript:

1 An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting

2 An Introduction to Java Programming and Object-Oriented Application Development 2 Brief Review of the char Data Type Recall a char is one of eight _________ types A character is any symbol represented by Unicode A char is always expressed inside single quotes Every individual character has an equivalent ________ value The methods of the Character class manipulate characters

3 An Introduction to Java Programming and Object-Oriented Application Development 3 Brief Review of the char Data Type (continued)

4 An Introduction to Java Programming and Object-Oriented Application Development 4 Methods in the Character Class The Character class is a __________ class The Integer and Double classes are also wrapper classes Each primitive type has a corresponding wrapper class A wrapper class ‘wraps’ the value of a primitive type within an object so methods can be applied

5 An Introduction to Java Programming and Object-Oriented Application Development 5 Methods in the Character Class (continued) charValue retrieves the character stored in the Character object compareTo returns the difference in _________ values for two characters equals is true if two characters are identical isDigit is true if a character is in [0-9] isLetter is true if a character is in [a-z or A-Z]

6 An Introduction to Java Programming and Object-Oriented Application Development 6 Methods in the Character Class (continued) isWhiteSpace is true if the character is a space isWhiteSpace returns false if the character is newline, tab, etc. toLowerCase changes uppercase to lowercase toUpperCase changes lowercase to uppercase

7 An Introduction to Java Programming and Object-Oriented Application Development 7 Brief Review of the String Class Recall a string is an object –String myStr = “Johnson” –String myStr = new String( “Johnson” ) Recall the methods equals, length and substring A string is a __________ variable that contains the memory address of an array of characters Two classes StringBuilder and StringTokenizer manipulate strings

8 An Introduction to Java Programming and Object-Oriented Application Development 8 Brief Review of the String Class (continued) StringBuilder provides methods to manipulate and build strings StringTokenizer breaks strings into _______ A token is a group of characters within a string Tokens are separated by ___________ The tokens in this sentence are delimited by white space

9 An Introduction to Java Programming and Object-Oriented Application Development 9 The StringBuilder Class A String object is ____________ –i.e., once created it can never be changed StringBuilder allows strings to be modified StringBuilder is in the java.lang package All methods in StringBuilder are _________ (i.e., can be called only by objects, not the class)

10 An Introduction to Java Programming and Object-Oriented Application Development 10 The StringBuilder Class (continued) append adds text to an existing StringBuilder object insert inserts a second argument just after the position given by the first argument deleteCharAt deletes a single character from a StringBuilder at a given position replace replaces all occurrences of one character with another toString creates a _______ that contains the data from the StringBuilder object

11 An Introduction to Java Programming and Object-Oriented Application Development 11 The StringTokenizer Class StringTokenizer is in the _________ package StringTokenizer breaks a string into tokens –Create the string to be tokenized –Create the StringTokenizer object linked to the string –Apply the StringTokenizer object to the string to get a token nextToken and countTokens are nonstatic

12 An Introduction to Java Programming and Object-Oriented Application Development 12 The printf and format Methods We have been using System.out.println and string concatenation ( + ) Instead of print and println, you can use printf and format

13 An Introduction to Java Programming and Object-Oriented Application Development 13 The printf and format Methods (continued)

14 An Introduction to Java Programming and Object-Oriented Application Development 14 The printf and format Methods (continued)

15 An Introduction to Java Programming and Object-Oriented Application Development 15 The printf and format Methods (continued) printf takes a format string and a string variable: printf(“Cost: $%.2f\n”, cost ); The format specifier begins with %, ends with a conversion character: %.2f Conversion suffix characters may follow the conversion characters Integers use conversion characters d and x Floating points use conversion characters e, E, f, and g

16 An Introduction to Java Programming and Object-Oriented Application Development 16 The printf and format Methods (continued) Characters and strings use the conversion characters C and S Dates and times use the conversion character t, possibly with suffix characters C, A, B, d, Y The argument index specifies the argument to which the format specifier applies –$1 is the first, $2 is the second,…, $n is the n th The field width is the number of spaces an argument occupies The precision is the number of decimal places

17 An Introduction to Java Programming and Object-Oriented Application Development 17 The format Method The printf method displays to the command window printf cannot be used to output to a GUI or file The format method in the Formatter class can output to a GUI or file format is identical to printf

18 An Introduction to Java Programming and Object-Oriented Application Development 18 The format Method (continued)


Download ppt "An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting."

Similar presentations


Ads by Google