Slide #1.

presentation slides for JAVA, JAVA, JAVA Object-Oriented Problem Solving Third Edition Ralph Morelli | Ralph Walde Trinity College Hartford, CT published by Prentice Hall
More slides like this


Slide #2.

Java, Java, Java Object Oriented Problem Solving Chapter 7: Strings and String Processing
More slides like this


Slide #3.

Objectives • Be more familiar with Java Strings. • Know how to solve problems that involve manipulating strings. • Be able to use loops in designing string processing algorithms. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #4.

Outline • • • • • • • • • • • Introduction Basics of the Java String Type Finding Things within a String Example: Keyword Search From the Java Library: StringBuffer Retrieving Parts of Strings Example: Processing Names and Passwords Processing Each Character in a String Comparing Strings From the Java Library: StringTokenizer Handling Text in a Graphics Context (Optional) Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #5.

Introduction • A data structure is a collection of data that is organized (structured) in some way. • A string is a collection of character (char) data. Strings are important data structures in a programming language • Strings are used to represent a wide variety of data. • In Java, the Object.toString() method represents an object as a string. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #6.

String Basics • A java.lang.String object is a sequence of characters plus a collection of methods for manipulating strings. • Unlike other Java objects, Strings have certain characteristics in common with the primitive data types. • For example, strings can have literals. A String literal is a sequence of zero or more characters contained in double quotes -- for example, “Socrates” and “” (empty string). Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #7.

The String Class Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #8.

Constructing Strings • The String constructors: public String(); public String(String initial_value); // Creates an empty string // Creates a copy of a string • The following constructor statement creates a String object and makes name a reference to it: String name = new String(); String instance variables. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #9.

Constructing Strings (cont) • A literal -- e.g., “Socrates” -- is considered a reference to a String object. All occurrences of “Socrates” in a program refer to the same object. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #10.

Constructing Strings (cont) • When literals are assigned to String variables Java makes those variables serve as references to the literals. String name1 = ""; String name2 = "Socrates"; String name3 = "Socrates"; // Reference to the empty string // References to "Socrates" Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #11.

Constructing Strings (cont) • New String objects are created whenever the String constructors are used: String name4 = new String(); String name5 = new String("Socrates"); String name6 = name4; Java, Java, Java, 3E by R. Morelli | R. Walde // Creates an object Copyright 2006. Chapter 7: Strings
More slides like this


Slide #12.

Concatenating Strings • When surrounded on either side by a String, the + symbol is used as a binary concatenation operator. It has the effect of joining two strings together. String lastName = "Onassis"; String jackie = new String("Jacqueline " + "Kennedy " + lastName); “Jacqueline Kennedy Onassis” • Primitive types are automatically promoted to strings when mixed with concatenation operators. System.out.println("The square root of 25 = " + 5); Output: The square root of 25 = 5 Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #13.

Indexing Strings • The number of characters in a string is its length. String String String String string1 string2 string3 string4 = = = = ""; // string1.length() ==> 0 "Hello"; // string2.length() ==> 5 "World"; // string3.length() ==> 5; string2 + " " + string3; // string4.length() ==> 11; • The position of a character within a string is called its index. Strings are zero indexed -- the first character is at index 0. Note: Because of zero indexing, the last character in a string of 8 characters is at index 7. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #14.

Converting Data to String • The String.valueOf() methods are class methods that convert primitive types into String objects. static public String valueOf( primitive type String String String String number = new String (String.valueOf(128)); truth = new String (String.valueOf(true)); bee = new String (String.valueOf('B')); pi = new String(String.valueOf(Math.PI)); Recall that one refers to class methods by using the class name as the qualifier. Java, Java, Java, 3E by R. Morelli | R. Walde // // // // ); Creates Creates Creates Creates "128" "true" "B" "3.14159" Note the difference between ‘B’ and “B” Copyright 2006. Chapter 7: Strings
More slides like this


Slide #15.

Finding Things within a String • The indexOf() and lastIndexof() methods are instance methods that can be used to find the index position of a character or a substring within a String. public public public public int int int int indexOf(int character); indexOf(int character, int startingIndex); indexOf(String string); indexOf(String string, int startingIndex); public public public public int int int int lastIndexOf(int character); lastIndexOf(int character, int startingIndex); lastIndexOf(String string); lastIndexOf(String string, int startingIndex); Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #16.

IndexOf() and LastIndexOf() • The indexOf() method searches from left to right within a String for either a character or a substring. • The lastIndexOf() method searches from right to left for a character or substring. String String String String string1 string2 string3 string4 = = = = ""; "Hello"; "World"; string2 + " " + string3; string1.indexOf('o') string2.indexOf('o') string3.indexOf('o') string4.indexOf('o') ==> ==> ==> ==> -1 4 1 4 string1.lastIndexOf('o') string2.lastIndexOf('o') string3.lastIndexOf('o') string4.lastIndexOf('o') ==> ==> ==> ==> -1 4 1 7 A return value of -1 means the character is not in the string. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #17.

IndexOf() and LastIndexOf() • The indexOf() and lastIndexOf() methods can also be used to find substrings, such as “or”. String String String String string1 string2 string3 string4 = = = = ""; "Hello"; "World"; string2 + " " + string3; string1.indexOf("or") string2.indexOf("or") string3.indexOf("or") string4.indexOf("or") ==> ==> ==> ==> -1 -1 1 7 string1.lastIndexOf("or") string2.lastIndexOf("or") string3.lastIndexOf("or") string4.lastIndexOf("or") ==> ==> ==> ==> -1 -1 1 7 A return value of -1 means that “or” is not in the string. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #18.

Example: Keyword Search • Finding a keyword within a string is a task that word processors and browsers must do. • Algorithm Design: Find every occurrence of some keyword, K, within a string, S: Suppose S is our string and K is the keyword. Initialize a counter variable and result string. Set P to the indexOf() the first occurrence of K in S. While ( P != -1 ) While loop because there may Increment the counter be 0 or more occurrences. Insert P into the result string Set P to the next location of the keyword in S Insert the count into the result string Return the result string as a String When P is -1, there are no more occurrences of K in S. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #19.

Implementation: Keyword Search /** * Pre: s and keyword are any Strings * Post: keywordSearch() returns a String containing the * number of occurrences of keyword in s, followed * by the starting location of each occurrence Bound: When ptr is */ -1, no more public String keywordSearch(String s, String keyword) { String resultStr = ""; occurrences of s. int count = 0; int ptr = s.indexOf(keyword); while (ptr != -1) { Initializer ++count; Updater resultStr = resultStr + ptr + " "; ptr = s.indexOf(keyword, ptr + 1); // Find next occurrence } resultStr = count + ": " + resultStr; // Insert the count return resultStr; // Return as a String } // keywordSearch() Test Performed keywordSearch( "this is a test","is") Expected Result 2: 2 5 keywordSearch( "able was i ere i saw elba","a") keywordSearch( "this is a test","taste") 4: 0 6 18 24 0: Java, Java, Java, 3E by R. Morelli | R. Walde Test data should test all possible outcomes. Copyright 2006. Chapter 7: Strings
More slides like this


Slide #20.

Automatic Garbage Collection • Immutability: Java Strings are cannot be modified. Whenever you assign a new value to a String, Java must create a new String object and discard the old one. In resultStr = resultStr + ptr + “ “; Java will create a new object (b) referenced by resultStr: The original resultStr object has no more references to it so it has to be garbage collected which takes time. Java, Java, Java, 3E by R. Morelli | R. Walde This is the new resultStr. Copyright 2006. Chapter 7: Strings
More slides like this


Slide #21.

Library: java.lang.StringBuffer • Objects of java.lang.StringBuffer class are strings that can be modified. Some of its methods are: Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #22.

The Revised keywordSearch() public String keywordSearch(String s, String keyword) { StringBuffer resultStr = new StringBuffer(); // Create StringBuffer int count = 0; int ptr = s.indexOf(keyword); while (ptr != -1) { ++count; resultStr.append(ptr + " "); // Insert letters into it ptr = s.indexOf(keyword, ptr + 1); The revised } resultStr.insert(0, count + ": "); keywordSearch() return resultStr.toString(); // Convert buffer back to a String uses a local } // keywordSearch() StringBuffer to build the result, but converts it back to String before returning. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #23.

Retrieving Parts of Strings • The String class contains methods to retrieve characters and substrings from a string. Takes the substring from startIndex to endIndex public char charAt(int index) public String substring(int startIndex) public String substring(int startIndex, int endIndex) Takes the substring from startIndex to the end of string. Note: endIndex points to index after the last character taken. String str = "HelloWorld"; str.substring(5) ==> "World" str.substring(3) ==> "loWorld"; // 0123456789 String str = "HelloWorld"; str.substring(5,7) ==> "Wo" str.substring(0,5) ==> "Hello"; str.substring(5, str.length()) ==> "World" Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #24.

Example: Names and Passwords • Problem: Suppose user names and passwords are stored as delimited strings where ‘:’ is the delimiter. Write methods to get the name and password. • Algorithm: Use the indexOf() and substring() methods. smith:bg1s5xxx mccarthy:2ffo900ssi public String getName(String str) { int posColon = str.indexOf(':'); // Find the delimiter String result = str.substring(0, posColon); // Extract the name return result; These 3 could be a single statement: } return str.substring(0, str.indexOf(‘:’)); public String getPassword(String str) { int posColon = str.indexOf(':'); // Find the delimiter String result = str.substring(posColon + 1); // Extract the password return result; } Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #25.

Processing Each Character in a String • For example, suppose you want to count the number of occurrences of a certain character in a string: Use the string’s length as a bound. // countChar counts the number of ch’s in str // Precondition: Neither str nor ch are null // Postcondition: countchar() == the number of ch in str public int countChar(String str, char ch) { int counter = 0; // Initialize a counter for (int k = 0; k < str.length(); k++) // For each character if (str.charAt(k) == ch) // If it's a ch counter++; // count it return counter; // Return the result } Possible off-by-one Error: Remember that the last character in a string is at index length()-1. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #26.

Method Design: reverse() • reverse() reverses the letters in its String parameter. A StringBuffer is used to construct the reverse of s. /** * Pre: s is any non null string * Post: s is returned in reverse order */ public String reverse(String s) { StringBuffer result = new StringBuffer(); for (int k = s.length() -1; k >= 0; k--) { result.append(s.charAt(k)); } //for return result.toString(); } // reverse() The result must be converted back to a String. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #27.

Method Design: capitalize() • capitalize() converts its parameter into a string with an initial capital letter (Capitalize). Note use of toUpperCase(char) method. /** * Pre: s is any non null string * Post: s is returned with only its first letter capitalized */ public String capitalize(String s) { if (s.length() == 0) return s; StringBuffer result = new StringBuffer(); result.append(toUpperCase(s.charAt(0))); // Change first to UPPERCASE for (int k = 1; k < s.length(); k++) { // Convert every other letter result.append(toLowerCase(s.charAt(k))); } //for return result.toString(); } // capitalize() private char toUpperCase(char ch) { if ((ch >= ‘a’) && (ch <= ‘z’)) return (char)(ch - 32); // Explicit cast required return ch; } Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #28.

Miscellaneous String Methods • The String class contains additional useful string manipulation methods, including the following: Method Signature boolean endsWith( String suffix) boolean startsWith(String prefix) String toUpperCase() String toLowerCase() String trim() Example "Perfection".endsWith("tion") "Perfection".startsWith("Per") "Perfection".toUpperCase() "Perfection".toLowerCase() " Perfection ".trim() Value true true "PERFECTION" "perfection" "Perfection" • Note that methods such as toUpperCase(), toLowerCase(), and trim() produce a new String object because Strings cannot be modified. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #29.

Comparing Strings • Strings are compared according to their lexicographic order. • Def: For strings s1 and s2, s1 precedes s2 in lexicographic order if its first character precedes the first character of s2. If their first characters are equal, then s1 precedes s2 if its second character precedes the second character of s2, and soon. Finally, the empty string is handled as a special case, preceding all other strings. • The following strings are arranged in lexicographic order: " " "!" "0" "A" "Andy" "Z" "Zero" "a" "an" "and" "andy" "candy" "zero" Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #30.

Lexicographic Order • A Java method that implements our definition of lexicographic order. public boolean precedes(String s1, String s2) { int minlen = Math.min(s1.length(), s2.length()); // Pick shorter length int k = 0; // Start at the first character while (k < minlen) { // While more characters if (s1.charAt(k) < s2.charAt(k)) // If kth s1 < kth in s2 return true; // then s1 precedes s2 else if (s2.charAt(k) < s1.charAt(k)) // If kth in s2 < kth in s1 return false; // s1 does not precede s2 else // If neither case k++; // go on to the next character } // while return s1.length() < s2.length(); // If all characters so far are equal } // precedes() // s1 < s2 if it is shorter than s2 Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #31.

String Identity vs. String Equality • Methods for comparing strings: public boolean equals(Object anObject); // Overrides Object.equals() public boolean equalsIgnoreCase(String anotherString) public int compareTo(String anotherString) • Two strings are equal if they have the same letters in the same order: String s1 = "hello"; String s2 = "Hello"; s1.equals(s2) // false s1.equals("hello”); // true • Error: Using == to compare two strings. For objects, o1 == o2 means o1 and o2 are identical. • The == operator is equivalent to Object.equals() method: o1.equals(o2) means o1 and o2 are identical. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #32.

String Identity vs. String Equality (cont) Given the following declarations String String String String String String s1 s2 s3 s4 s5 s6 = = = = = = new String("hello"); new String("hello"); new String("Hello"); s1; // s1 == s4 "hello"; "hello"; We get the following equality results s1.equals(s2) s1.equals(s3) s1.equalsIgnoreCase(s3) s1.equals(s4) s1.equals(s5) s1.equals(s6) ==> ==> ==> ==> ==> ==> true false true true true true And the following identity results s5 and s6 refer to the same (identical) literal object. s1 s1 s1 s1 s5 == == == == == Java, Java, Java, 3E by R. Morelli | R. Walde s2 s3 s4 s5 s6 ==> ==> ==> ==> ==> Copyright 2006. Chapter 7: Strings false false true false true
More slides like this


Slide #33.

String Identity vs. String Equality (cont) • In this figure, s1, s2, s4, s5, and s6 are equal. • Strings s1 and s4 are identical, as are s5 and s6. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #34.

Library: java.util.StringTokenizer • Break up a string into its tokens -- e.g., breaking up a name and password pair in boyd:14irXp. • The StringTokenizer class is designed for this purpose. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #35.

StringTokenizer (cont) • A StringTokenizer breaks a string into tokens separated by delimiters, which by default value are the whitespace characters: StringTokenizer sTokenizer = new StringTokenizer("This is an English sentence."); In this case, the period is part of last token Tokens This is an English sentence. • The delimiters can be specified as a String parameter: StringTokenizer sTokenizer = new StringTokenizer("http://troy.trincoll.edu/~jjj/index.html”, ":/"); Tokens http troy.trincoll.edu ~jjj index.html Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #36.

StringTokenizer (cont) • A while loop can be used to process each token in the tokenizer: String url = "http://troy.trincoll.edu/~jjj/index.html"; StringTokenizer sTokenizer = new StringTokenizer(url, ":/"); // Initializer while (sTokenizer.hasMoreTokens()) { System.out.println(sTokenizer.nextToken()); } // Loop entry condition // Updater prints http troy.trincoll.edu ~jjj index.html • In this example, the StringTokenizer() constructor initializes loop, the hasMoreTokens() method serves as the loop entry condition, and the nextToken() method is the updater. Of course, the StringTokenizer object keeps track of the tokens in its internal state. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #37.

Handling Text in a Graphics Context • Each graphics context has an associated Font and FontMetrics object, accessible by: • A FontMetrics encapsulates font data -- e.g., height and width. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #38.

The Font Class • The Font class provides a platformindependent representation of an individual font. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #39.

The FontMetrics Class • The FontMetrics class has methods for calculating font sizes. Get the width of a character Get the font’s height. Or the width of a string. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #40.

Example: Centering a Line of Text • Each time you resize the window, it picks a different font and centers “Hello World!” • Sourcecode/Demo: CenterText.java Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #41.

Implementation: CenterText • For any font, center a string in the window. No matter what font the system gives us ... import java.awt.*; import javax.swing.*; public class CenterText extends JFrame { // Print hello world! in center of frame public void paint(Graphics g) { String str = "Hello World!"; g.setFont(new Font("Random", Font.PLAIN, 24)); // Random font FontMetrics metrics = g.getFontMetrics(); // Get its metrics Dimension d = getSize(); // Get the frame's size // Clear the frame g.setColor(getBackground); g.fillRect(0,0,d.width,d.height); g.setColor(Color.black); // Calculate coordinates int x = (d.width - metrics.stringWidth(str)) / 2; int y = (d.height + metrics.getHeight()) / 2; g.drawString( str, x, y ); // Draw the string } // paint() } // CenterText … use its metrics to center it. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #42.

Technical Terms baseline data structure empty string lexicographic order orphan object string token concatenation delimited string garbage collection logical font physical font string index unit indexed Java, Java, Java, 3E by R. Morelli | R. Walde copy constructor delimiter glyph off-by-one error read only string literal zero indexed Copyright 2006. Chapter 7: Strings
More slides like this


Slide #43.

Summary Of Important Points • A String literal is a sequence of 0 or more characters enclosed within double quote marks. A String object is a sequence of 0 or more characters, plus a variety of class and instance methods and variables. • The String concatenation operator is the overloaded + symbol; it is used to combine two strings into a single String : “hello” + “world” ==> “helloworld”. • Strings are indexed starting at 0 (zero indexing). Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #44.

Summary Of Important Points (cont) • The indexOf() and lastIndexOf() methods are used for finding the first or last occurrence of a character or substring within a String. • The valueOf() methods are used to convert a nonstring into a String. • The length() method is used to determine the number of characters in a String. • The charAt() method is used to return the single character at a particular index position. • The various substring() methods are used to return the substring at particular index positions in a String. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this


Slide #45.

Summary Of Important Points (cont) • The overloaded equals() method returns true if two Strings contain the same exact sequence of characters. The == operator, when used on Strings, returns true if two references designate the same String object. • A StringTokenizer is an object that can be used to break a String into a collection of tokens separated by delimiters. The whitespace characters, -- tabs, blanks, and newlines -- are the default delimiters. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 7: Strings
More slides like this