Date post: | 11-Oct-2015 |
Category: |
Documents |
Upload: | inconspicuousannie |
View: | 415 times |
Download: | 1 times |
5/21/2018 Chapter 8: Starting with Java
1/54
TOPICS
8 1 Introduction to Wrapper Classes
8 2 Character Testing and Conversion with
the
Character
Class
8 3 More about
Str ing
Objects
8 4 The StringBuilder
Class
8 5 Tokenizing Strings
8 6 Wrapper Classes for the Numeric
Data Types
8 7 Focus on Problem Solving: The
TestScoreReader
Class
88 Common
Errors
t Avoid
Introduction
to
Wrapper lasses
CONCEPT Java provides wrapper classes for the primitive data types. The wrapper
class for a given primitive type contains not only a value of that type, but
also methods that perform operations related to the type.
Recall from Chapter 2 that the primitive data types are called primi tive because they
are not created from classes. Instead of instantiating objects, you create variables from the
primitive data types, and variables do not have attributes or methods . They are designed
simply to hold a single value in memory.
Java also provides wrapper classes for all of the primitive data types. A wr pper cl ss
is
a class that is wrapped around a primitive data type and allows you to create objects
instead
of
variables. In addition, these wrapper classes provide methods
that
perform useful
operations on primitive values.
Although these wrapper classes can be used to create objects instead
of
variables, few
programmers use them that way. One reason
is
because the wrapper classes are immu-
table, which means that once you create an object, you cannot change the object's value.
Another reason is because they are not as easy to use as variables for simple operations. For
example, to get the value stored in
an
object you must call a method, whereas variables can
be used directly in assignment statements, passed as arguments to the pr n t
and
pr nt ln
methods, and so forth .
49
5/21/2018 Chapter 8: Starting with Java
2/54
49
Chapter
8 Text Processing and Wrapper Classes
Although it is not normally useful to create objects from the wrapper classes, they do provide
static methods that are very useful.
We
examine several of Java s wrapper classes in this chapter.
We begin by looking
at
the
Character
class, which is the wrapper class for the char data type.
haracter Testing and onversion with the
haracter lass
CONCEPT: The haracter class
is
a wrapper class for the
char
data type . t
provides numerous methods for testing and converting character data.
The Character class
is
part of the j ava . l ang package, so no i m
port
statement is necessary
to
use this class. The class provides several static methods for testing the value
of
a ch
ar
variable. Some of these methods are listed in Table 8-1. Each of the methods accepts a single
char argument and returns a boo l ean value.
Table 8 1
Some static
Character
class
methods
for testing
char
values
Method
boolean isDigit char ch
boolean
isLetter char
ch
boolean
isLetterOrDigit char
ch
boolean isLowerCase(char ch
bool ean i sUpperCase(char ch
bool ean i sSpaceChar char ch
bool ean i hiteSpace char ch
Description
Returns true if the
argument
passed
into
ch
is
a
digit from 0 through 9. Otherwise returns fa l se.
Returns true if the argument passed into ch
is
an
alphabetic letter. Otherwise returns fa l se.
Returns
true
if the character passed into
ch
con
tains a digit (0 through 9) or an alphabetic letter.
Otherwise returns
false.
Returns ue if the argument passed into ch
is
a
lowercase letter. Otherwise returns fa l se.
Returns ue if the argument passed into ch
is
an
uppercase letter. Otherwise returns fa l se.
Returns
tru
e if the argument passed into ch
is
a
space character. Otherwise returns fal se.
Returns tr ue if the argument passed into ch
is
a whitespace character (a space, tab,
or
newline
character). Otherwise returns fal se.
5/21/2018 Chapter 8: Starting with Java
3/54
8.2 Character Testing
and
Conversion with the
Character
Class 493
The program in Code Listing 8-1 demonstrates many of these methods.
Code Listing
8
CharacterTest.java)
1
import
java .ut i l .Sca
nner
;
2
1
4
5
6
7
This
program
demonstrates some
of the Character
class ' s
character
test ing methods.
1
8 public class
CharacterTest
9
10 public sta t ic void main(String[] args)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
String inputLine ;
char inputChar;
II A l ine of input
II A character
II Create a Scanner
object
for keyboard input .
Scanner keyboard = new
Scanner(System.in);
II Get a
character
from the user .
System. out.
print Enter a character: );
inputLine
keyboard
. nextLine() ;
inputChar
= inputLine
.c
harAt(O) ;
II
Test the character.
i (Character.isLetter(inputChar))
System.out.println( Letter ) ;
i f
(Character
.
isDigit(inputChar))
System .o
ut . print ln( Digi t ) ;
i f
(Character.isLowerCase(inputChar))
System .out . println( Lowercase le t ter );
i C
haracter
. isUpperCase(inputChar))
System.out.println(
Uppercase
let ter )
;
i f (Character . isSpaceChar(inputChar))
System.out.println( Space );
i
(Character.isWhitespace(inputChar))
System. out.
print ln Whitespace ) ;
5/21/2018 Chapter 8: Starting with Java
4/54
494 Chapter
8 Text Processing
and
Wrapper Classes
Program Output with Example Input Shown in old
Enter a character :
~ n t e ~
et ter
Lowercase l e t t e r
Program Output
with
Example Input
Shown
in old
Enter a character : A [Enter]
et ter
Uppercase
l e t t e r
Program Output
with
Example Input Shown in old
Enter
a
character : [Enter]
Digi t
Program Output
with
Example Input Shown in old
Enter a character :
[ pace]
[Enter]
Space
Whitespace
character
Program Output
with
Example Input Shown in old
Enter
any
character : [Tab] [Enter]
Whitespace character
Code Listing 8 2 shows a
more
practical application of the character testing methods. t
tests a string to determine whether it is a seven character customer number in the proper
format.
Code Listing
8-2 CustomerNumber.java)
1 import java.ut i l . Scanner;
2
3 1
4
This
program
tests
a
customer
number
to
determine
5
whether i t
is in the proper
format
.
6 1
7
8 public
class
CustomerNumber
9
1 public sta t ic
void
main String[] args)
11
12 String
customer;
To hold
a customer number
13
5/21/2018 Chapter 8: Starting with Java
5/54
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
8.2 Character Testing
and
Conversion with the
Character
Class 95
II Create a Scanner object
for
keyboard input .
Scanner keyboard
= new
Scanner(System.in);
System.out.println( Enter a customer number in "
+
the
form LLLNNNN );
System.out.print( (LLL
= le t ters and NNNN "
+ "= numbers): );
II Get a customer number from the user .
customer =
keyboard
.
nextLine();
II Determine whether i t is valid.
i f (isValid(customer))
System.out.println( That's
a valid
customer
+ number. );
31
else
32
33
34
35
36
37
38
39
40 1
System. out.
print ln(
That is not the proper
+ format. );
System.out.println( "Here is an example: "
+ "ABCI234");
41 The iSValid method
accepts
a
String
as i t s
argument
42 and tes ts
i ts contents
for a valid customer number .
43 1
44
45 private st t ic
boolean isValid(String
custNumber)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
boolean
goodSoFar
int
index
= 0;
true;
Flag
II Loop control variable
Is the str ing the correct length?
i f (custNumber.length() = 7)
goodSoFar = fa lse;
II Test the f i r s t three characters for
le t ters
.
while (goodSoFar
index
< 3)
i f ( C h a r a c t e r . i s L e t t e r ( C U S ~ a r A t ( i n d e X ) ) )
goodSoFar = fa lse;
index++;
5/21/2018 Chapter 8: Starting with Java
6/54
96 Chapter 8 Text Processing and Wrapper Classes
6 Test the last four characters for digits .
63
while
(goodSoFar
index