Java Reference
In-Depth Information
tion on class
Character
(and all the type-wrapper classes), see the
java.lang
package in
the Java API documentation.
Figure 14.15 demonstrates
static
methods that test characters to determine whether
they're a specific character type and the
static
methods that perform case conversions on
characters. You can enter any character and apply the methods to the character.
1
// Fig. 14.15: StaticCharMethods.java
2
// Character static methods for testing characters and converting case.
3
import
java.util.Scanner;
4
5
public
class
StaticCharMethods
6
{
7
public
static
void
main(String[] args)
8
{
9
Scanner scanner =
new
Scanner(System.in);
// create scanner
10
System.out.println(
"Enter a character and press Enter"
);
11
String input = scanner.next();
12
char
c = input.charAt(
0
);
// get input character
13
14
// display character info
15
System.out.printf(
"is defined: %b%n"
,
Character.isDefined(c)
Character.isDigit(c)
);
16
System.out.printf(
"is digit: %b%n"
,
);
17
System.out.printf(
"is first character in a Java identifier: %b%n"
,
18
Character.isJavaIdentifierStart(c)
);
19
System.out.printf(
"is part of a Java identifier: %b%n"
,
20
Character.isJavaIdentifierPart(c)
);
21
System.out.printf(
"is letter: %b%n"
,
Character.isLetter(c)
);
22
System.out.printf(
23
"is letter or digit: %b%n"
,
Character.isLetterOrDigit(c)
);
24
System.out.printf(
25
"is lower case: %b%n"
,
Character.isLowerCase(c)
);
26
System.out.printf(
27
"is upper case: %b%n"
,
Character.isUpperCase(c)
);
28
System.out.printf(
29
"to upper case: %s%n"
,
Character.toUpperCase(c)
);
30
System.out.printf(
31
"to lower case: %s%n"
,
Character.toLowerCase(c)
);
32
}
33
}
// end class StaticCharMethods
Enter a character and press Enter
A
is defined: true
is digit: false
is first character in a Java identifier: true
is part of a Java identifier: true
is letter: true
is letter or digit: true
is lower case: false
is upper case: true
to upper case: A
to lower case: a
Fig. 14.15
|
Character
static
methods for testing characters and converting case. (Part 1 of 2.)