Text Processing and More About Wrapper Classes
Text Processing and More About Wrapper Classes
Chapter Topics
Chapter 9 discusses the following main topics:
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-2
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-3
Wrapper Classes
Wrapper classes allow you to create objects to
represent a primitive.
Wrapper classes are immutable, which means that once
you create an object, you cannot change the objects
value.
To get the value stored in an object you must call a
method.
Wrapper classes provide static methods that are very
useful
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-4
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-5
Description
boolean isDigit(
char ch)
boolean isLetter(
char ch)
boolean isLetterOrDigit(
char ch)
boolean isLowerCase(
char ch)
boolean isUpperCase(
char ch)
boolean isSpaceChar(
char ch)
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-6
Description
char toLowerCase(
char ch)
char toUpperCase(
char ch)
9-7
Substrings
The String class provides several methods that search for a
string inside of a string.
A substring is a string that is part of another string.
Some of the substring searching methods provided by the
String class:
boolean startsWith(String str)
boolean endsWith(String str)
boolean regionMatches(int start, String str, int start2,
int n)
boolean regionMatches(boolean ignoreCase, int start,
String str, int start2, int n)
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-8
Searching Strings
The startsWith method determines whether a
string begins with a specified substring.
String str = "Four score and seven years ago";
if (str.startsWith("Four"))
System.out.println("The string starts with Four.");
else
System.out.println("The string does not start with Four.");
9-9
Searching Strings
The endsWith method determines whether a string
ends with a specified substring.
String str = "Four score and seven years ago";
if (str.endsWith("ago"))
System.out.println("The string ends with ago.");
else
System.out.println("The string does not end with ago.");
9-10
Searching Strings
The String class provides methods that will if
specified regions of two strings match.
regionMatches(int start, String str, int start2,
int n)
9-11
Searching Strings
The String class also provides methods that will
locate the position of a substring.
indexOf
returns the first location of a substring or character in the
calling String Object.
lastIndexOf
returns the last location of a substring or character in the
calling String Object.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-12
Searching Strings
String str = "Four score and seven years ago";
int first, last;
first = str.indexOf('r');
last = str.lastIndexOf('r');
System.out.println("The letter r first appears at "
+ "position " + first);
System.out.println("The letter r last appears at "
+ "position " + last);
String str = "and a one and a two and a three";
int position;
System.out.println("The word and appears at the "
+ "following locations.");
position = str.indexOf("and");
while (position != -1)
{
System.out.println(position);
position = str.indexOf("and", position + 1);
}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-13
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-14
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-15
Extracting Substrings
The String class provides methods to extract
substrings in a String object.
The substring method returns a substring beginning
at a start location and an optional ending location.
String fullName = "Cynthia Susan Smith";
String lastName = fullName.substring(14);
System.out.println("The full name is "
+ fullName);
System.out.println("The last name is "
+ lastName);
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-16
Extracting Substrings
The fullName variable holds
the address of a String object.
Address
Smith
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-17
toCharArray
Returns the String objects contents in an array of char values.
Example: StringAnalyzer.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-18
replace
Returns a String object with all occurrences of one character
being replaced by another character.
trim
Returns a String object with all leading and trailing whitespace
characters removed.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-19
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-20
9-21
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-22
StringBuilder Constructors
StringBuilder()
This constructor gives the object enough storage space to hold 16
characters.
StringBuilder(int length)
This constructor gives the object enough storage space to hold
length characters.
StringBuilder(String str)
This constructor initializes the object with the string in str.
The object will have at least enough storage space to hold the string in
str.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-23
9-24
9-25
9-26
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-27
object.insert(start, item);
where object is an instance of the StringBuilder
class, start is the insertion location, and item is:
a primitive literal or variable.
a char array, or
a String literal or object.
Example:
Telephone.java
TelephoneTester.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-28
9-29
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-30
9-31
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-33
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-34
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-35
StringTokenizer Constructors
Constructor
Description
StringTokenizer(
String str)
StringTokenizer(
String str,
String delimiters)
StringTokenizer(
String str,
String delimiters,
Boolean returnDelimeters)
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-36
9-37
StringTokenizer Methods
The StringTokenizer class provides:
countTokens
Count the remaining tokens in the string.
hasMoreTokens
Are there any more tokens to extract?
nextToken
Returns the next token in the string.
Throws a NoSuchElementException if there are no more
tokens in the string.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-38
Extracting Tokens
Loops are often used to extract tokens from a string.
StringTokenizer strTokenizer =
new StringTokenizer("One Two Three");
while (strTokenizer.hasMoreTokens())
{
System.out.println(strTokenizer.nextToken());
}
9-39
Multiple Delimiters
The default delimiters for the StringTokenizer class
are the whitespace characters.
\n\r\t\b\f
9-40
Multiple Delimiters
To extract the tokens from this string we must specify both
characters as delimiters to the constructor.
StringTokenizer strTokenizer =
new StringTokenizer("joe@gaddisbooks.com", "@.");
while (strTokenizer.hasMoreTokens())
{
System.out.println(strTokenizer.nextToken());
}
9-41
9-42
Numeric Primitive
Type It Applies To
Byte
byte
Double
double
Float
float
Integer
int
Long
long
Short
short
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-43
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-44
9-45
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-46
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-47
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-48
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-49
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-50
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-51
Problem Solving
Dr. Harrison keeps student scores in an Excel file.
This can be exported as a comma separated text file.
Each students data will be on one line. We want to
write a Java program that will find the average for
each student. (The number of students changes each
year.)
Solution: TestScoreReader.java, TestAverages.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9-52