As many suggested, you can use indexOf
or ||
. I'll not repeat myself. You can use something as follows as well,
public class VowelOrNot {
public static void main(String[] args) {
String valor = "z";
switch (valor.toLowerCase()) {
case "a":
case "e":
case "i":
case "o":
case "u":
System.out.println(valor + " is vowel");
break;
default:
System.out.println(valor + " is consonant");
}
}
}
I'm using switch case. Which is handy when you want to avoid if-else/if-if ladder in multiple scenarios. Basically, if the input is matched with any of the case then it prints that string is a vowel else it is a consonant. Note, that break
is important. Otherwise it will check default case and print consonant if given valor
is a vowel.
If you want, you replace String
with char
in the variable declaration and handle it appropriately in switch cases.