-4

How can I test if a single character is a vowel, using a single if statement?

More practically, how can I consolidate my current logic below into a single if statement?

if (valor == 'a'); System.out.println("xdd");
if (valor == 'e'); System.out.println("xdd");
if (valor == 'i'); System.out.println("xdd");
if (valor == 'o'); System.out.println("xdd");
if (valor == 'u'); System.out.println("xdd");
esqew
  • 42,425
  • 27
  • 92
  • 132
Legoruu
  • 1
  • 1
  • Whats wrong with a switch case? https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – papaya Oct 20 '20 at 16:54
  • 5
    That semicolon after if` is misplaced. This won’t work. – Konrad Rudolph Oct 20 '20 at 16:54
  • 1
    Duplicate of [can you have two conditions in an if statement](https://stackoverflow.com/questions/44830259/can-you-have-two-conditions-in-an-if-statement) – esqew Oct 20 '20 at 16:55
  • You can use a [switch case](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html), or [conditional operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html) – JustAnotherDeveloper Oct 20 '20 at 16:55
  • Generally speaking, 'aeiou' are not the only vowels in the world. Is this US-only code? – user14387228 Oct 20 '20 at 17:08
  • @user14387228 Yeah, but he only wants to know how to test characters with a single if statement. How he uses his new found powers is up to him. Will it be for good, or for awesome? All I know is we are all in his origin story now! –  Oct 20 '20 at 17:13

8 Answers8

3

Try this. This will work whether valor is a character or a string.

if ("aeiou".indexOf(valor) >= 0) {
    System.out.println("xdd");
}
WJS
  • 36,363
  • 4
  • 24
  • 39
3

You can try String.indexOf()

final String vowels = "aeiou";
char valor = 'e';
if (vowels.indexOf(valor) != -1) {
    System.out.println("xdd");
}
rkosegi
  • 14,165
  • 5
  • 50
  • 83
1

You can leverage the || operator which basically serves as a or in a statement like so:

if(valor=='a'||valor=='e'||valor=='i'||valor=='o'||valor=='u'){
    System.out.println("xdd");
}

Since in this case, it will always print the same thing.

Spectric
  • 30,714
  • 6
  • 20
  • 43
  • This is what i was looking for, ty men,rest of answers are great but not as simple as this one – Legoruu Oct 20 '20 at 17:00
  • @Legoruu If my answer answers your question, please consider accepting so future visitors can be assisted – Spectric Oct 20 '20 at 17:02
1

One easy method, which can also be easily expanded, is using an index string and checking if your variable is part of that String.

String vowels = "aeiou";
if (vowels.indexOf(valor) >= 0) {
    // do whatever you have to
}
1

Let valor is a character converted to String, then:

if ("AaEeIiOoUu".contains(valor)) {
    System.out.println("xdd");
}
roddar92
  • 353
  • 1
  • 4
  • `String#contains` requires a `CharSequence` argument, not a single `char`. And called with a `CharSequence` it would also yield `true` for e.g. `"ioO"`, which it shouldn’t. – Konrad Rudolph Oct 20 '20 at 17:20
  • @KonradRudolph The OP said *How can I test if a single character is a vowel,* So based on the OP's statement, it would be a `CharSequence` of one character. So `ioO` is not relevant. – WJS Oct 20 '20 at 18:32
  • @WJS In OP’s code the type of `valor` is `char`. If it *isn’t* char, the test should naturally ensure that it’s only a single character long, this can’t just be assumed as given. – Konrad Rudolph Oct 20 '20 at 20:02
0

Use a regex.

String str = "a";

if (str.matches("[AaeEiIoOuU]") {
    System.out.println("It's a vowel!");
}
 else {
   System.out.println("it's not a vowel!");
}
  • Ah, totally right. I thought the only restriction fo `matches` is that it matches at the beginning but it also has to match the *entire* string. The behaviour of Python’s `re.match` is confusingly different, that’s why I misremembered. – Konrad Rudolph Oct 20 '20 at 17:06
0

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.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
0

You can easily achieve this by using the if conditional statements with java logical OR operator.

Using short circuit OR operator

This will be more efficient because this will return true at the first point when java meets the satisfying condition while checking the logical condition.

if(valor=='a'||valor=='e'||valor=='i'||valor=='o'||valor=='u'){
    System.out.println("xdd"); 
}

Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37