I made this method in java that replaces the vowels of a String if it contains letter 'i':
public static boolean esVocal(Character c) {
boolean res = false;
if (c == 'a' | c == 'o' | c == 'e' | c == 'u') {
res = true;
}
}
Anyway, the error that gives me is:
illegal start of expression
And at the end, it says that the method requires a return
.
Could the error be in the syntax?
Your method signature
public static boolean esVocal(Character c)
declares the return type boolean
. This means your method is supposed to return something of type boolean
.
If you don't want to return anything you should declare the return type as void
:
public static void esVocal(Character c)
Or if you want to return something you need to add the corresponding statement to your method:
public static boolean esVocal(Character c) {
boolean res = false;
if (c == 'a' | c == 'o' | c == 'e' | c == 'u') {
res = true;
}
// You probably wanted to return this
return res;
}
Note that you can reduce your method by directly returning the resulting boolean
like:
public static boolean esVocal(Character c) {
return c == 'a' | c == 'o' | c == 'e' | c == 'u';
}
The operator |
on numbers is not the logical or. It is a bit-wise inclusive or operation which makes some bit-manipulations on values like:
0100 1110
or 1000 1011
---------
1100 1111
However on boolean
expressions, like in your case, it also performs as logical or (see JLS 15.22.2).
There is also the operator ||
which always behaves as logical or.
Note that there is a difference between both operators, even when using on boolean
values. The |
operator will always compute the whole expression, even when the resulting value already is clear. The ||
aborts evaluation if the result already is clear like in
true || someBoolean || someOtherBoolean
The true
already makes the whole result true
and ||
will thus not evaluate the rest of the expression whereas |
will.
In your case however you probably want to use ||
:
c == 'a' || c == 'o' || c == 'e' || c == 'u'
For the logical and &
, &&
the difference is the same.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments