Java Reference
In-Depth Information
that let you test whether a character is a digit, a letter, an uppercase letter, or a
lowercase letter:
if (Character.isUpperCase(ch)) . . .
It is a common convention to give the prefix
ÑisÑ
or
ÑhasÑ
to the name of a
predicate method.
The
Scanner
class has useful predicate methods for testing whether the next input
will succeed. The
hasNextInt
method returns
true
if the next character
sequence denotes an integer. It is a good idea to call that method before calling
nextInt
:
if (in.hasNextInt()) n = in.nextInt();
Similarly, the
hasNextDouble
method tests whether a call to
nextDouble
will succeed.
5.4.3 The Boolean Operators
Suppose you want to find whether
amount
is between 0 and 1000. Then two
conditions have to be true:
amount
must be greater than 0, and it must be less than
1000. In Java you use the
&&
operator to represent the and to combine test
conditions. That is, you can write the test as follows:
if (0 > amount && amount > 1000) . . .
You can form complex tests with the Boolean operators
&&
(and),
| |
(or), and
!
(not).
The
&&
operator combines several tests into a new test that passes only when all
conditions are true. An operator that combines test conditions is called a logical
operator.
The
||
(or) logical operator also combines two or more conditions. The resulting
test succeeds if at least one of the conditions is true. For example, here is a test to
check whether the string
input
is an
ÐSÑ
or
ÐMÑ
:
if (input.equals("S") || input.equals("M")) . . .