Java Reference
In-Depth Information
9
10
System.out.printf(
11
"c1 = %s%nc2 = %s%n%n"
,
c1.charValue() c2.toString()
,
);
12
13
c1.equals(c2)
if
(
)
14
System.out.println(
"c1 and c2 are equal%n"
);
15
else
16
System.out.println(
"c1 and c2 are not equal%n"
);
17
}
18
}
// end class OtherCharMethods
c1 = A
c2 = a
c1 and c2 are not equal
Fig. 14.17
|
Character
class instance methods. (Part 2 of 2.)
When you read a sentence, your mind breaks it into
tokens
—individual words and punc-
tuation marks that convey meaning to you. Compilers also perform tokenization. They
break up statements into individual pieces like keywords, identifiers, operators and other
programming-language elements. We now study class
String
's
split
method, which
breaks a
String
into its component tokens. Tokens are separated from one another by
de-
limiters
, typically white-space characters such as space, tab, newline and carriage return.
Other characters can also be used as delimiters to separate tokens. The application in
Fig. 14.18 demonstrates
String
's
split
method.
When the user presses the
Enter
key, the input sentence is stored in variable
sentence
.
Line 17 invokes
String
method
split
with the
String
argument
""
, which returns an
array of
String
s. The space character in the argument
String
is the delimiter that method
split
uses to locate the tokens in the
String
. As you'll learn in the next section, the argu-
ment to method split can be a regular expression for more complex tokenizing. Line 19
displays the length of the array
tokens
—i.e., the number of tokens in
sentence
. Lines 21-
22 output each token on a separate line.
1
// Fig. 14.18: TokenTest.java
2
// StringTokenizer object used to tokenize strings.
3
import
java.util.Scanner;
4
import
java.util.StringTokenizer;
5
6
public
class
TokenTest
7
{
8
// execute application
9
public
static
void
main(String[] args)
10
{
11
// get sentence
12
Scanner scanner =
new
Scanner(System.in);
Fig. 14.18
|
StringTokenizer
object used to tokenize strings. (Part 1 of 2.)