How do I check if a char is a vowel?
Asked Answered
T

8

19

This Java code is giving me trouble:

    String word = <Uses an input>
    int y = 3;
    char z;
    do {
        z = word.charAt(y);
         if (z!='a' || z!='e' || z!='i' || z!='o' || z!='u')) {
            for (int i = 0; i==y; i++) {
                wordT  = wordT + word.charAt(i);
                } break;
         }
    } while(true);

I want to check if the third letter of word is a non-vowel, and if it is I want it to return the non-vowel and any characters preceding it. If it is a vowel, it checks the next letter in the string, if it's also a vowel then it checks the next one until it finds a non-vowel.

Example:

word = Jaemeas then wordT must = Jaem

Example 2:

word=Jaeoimus then wordT must =Jaeoim

The problem is with my if statement, I can't figure out how to make it check all the vowels in that one line.

Technique answered 3/10, 2013 at 13:47 Comment(11)
z!='a'||z!='e'... will always fail. z can't be equal to a and e (and so on) at the same time. Try && instead.Unproductive
I wanted the if to check that it wasnt 'a' 'e' 'i' 'o' 'u', how do I structure that if statement?Technique
It's this one. But you seen AND.Unproductive
Don't forget yvowel ! And I'm a little bit confused about your if condition : every letter is differeent from a OR e etc... Use AND operator insteadAshur
@X.L.Ant Isn't || the OR logic? I guess that statement will pass.Worldling
@Worldling Yeah sorry, I said 'fail', that's the other way around. The point is that this test is flawed.Unproductive
btw: !(a && B) is (!a || !b)Jail
Yeah thanks guys I sorted it out, it was supposed to us an AND not an OR. However if you see the answer I selected the "AEIOUaeiou".indexOf(z) <0 is a much cleaner solutionTechnique
@KyleMHB Those nots make the logic weird -- what bit you was DeMorgan's Laws. Good thing to refresh yourself on!Wavelet
@KyleMHB Please don't answer your question by editing it. If you have found a solution, post it as an answer to your own question.Tomas
Ok good to know I will do, thank you.Technique
C
38

Your condition is flawed. Think about the simpler version

z != 'a' || z != 'e'

If z is 'a' then the second half will be true since z is not 'e' (i.e. the whole condition is true), and if z is 'e' then the first half will be true since z is not 'a' (again, whole condition true). Of course, if z is neither 'a' nor 'e' then both parts will be true. In other words, your condition will never be false!

You likely want &&s there instead:

z != 'a' && z != 'e' && ...

Or perhaps:

"aeiou".indexOf(z) < 0
Caeoma answered 3/10, 2013 at 13:51 Comment(3)
Ah yes! Thank you so much. Cant believe I couldn't figure that out!Technique
Done! Used the indexOf() function and its much cleaner.Technique
Upvoted for using indexOf() which is way more flexible for other uses.Airing
A
70

Clean method to check for vowels:

public static boolean isVowel(char c) {
  return "AEIOUaeiou".indexOf(c) != -1;
}
Apoenzyme answered 3/10, 2013 at 13:57 Comment(0)
C
38

Your condition is flawed. Think about the simpler version

z != 'a' || z != 'e'

If z is 'a' then the second half will be true since z is not 'e' (i.e. the whole condition is true), and if z is 'e' then the first half will be true since z is not 'a' (again, whole condition true). Of course, if z is neither 'a' nor 'e' then both parts will be true. In other words, your condition will never be false!

You likely want &&s there instead:

z != 'a' && z != 'e' && ...

Or perhaps:

"aeiou".indexOf(z) < 0
Caeoma answered 3/10, 2013 at 13:51 Comment(3)
Ah yes! Thank you so much. Cant believe I couldn't figure that out!Technique
Done! Used the indexOf() function and its much cleaner.Technique
Upvoted for using indexOf() which is way more flexible for other uses.Airing
C
3

How about an approach using regular expressions? If you use the proper pattern you can get the results from the Matcher object using groups. In the code sample below the call to m.group(1) should return you the string you're looking for as long as there's a pattern match.

String wordT = null;
Pattern patternOne = Pattern.compile("^([\\w]{2}[AEIOUaeiou]*[^AEIOUaeiou]{1}).*");
Matcher m = patternOne.matcher("Jaemeas");
if (m.matches()) {
    wordT = m.group(1);
}

Just a little different approach that accomplishes the same goal.

Catechumen answered 3/10, 2013 at 15:1 Comment(0)
F
2

Actually there are much more efficient ways to check it but since you've asked what is the problem with yours, I can tell that the problem is you have to change those OR operators with AND operators. With your if statement, it will always be true.

Frida answered 3/10, 2013 at 13:59 Comment(0)
C
2

So in event anyone ever comes across this and wants a easy compare method that can be used in many scenarios.

Doesn't matter if it is UPPERCASE or lowercase. A-Z and a-z.

bool vowel = ((1 << letter) & 2130466) != 0;

This is the easiest way I could think of. I tested this in C++ and on a 64bit PC so results may differ but basically there's only 32 bits available in a "32 bit integer" as such bit 64 and bit 32 get removed and you are left with a value from 1 - 26 when performing the "<< letter".

If you don't understand how bits work sorry i'm not going go super in depth but the technique of

1 << N is the same thing as 2^N power or creating a power of two.

So when we do 1 << N & X we checking if X contains the power of two that creates our vowel is located in this value 2130466. If the result doesn't equal 0 then it was successfully a vowel.

This situation can apply to anything you use bits for and even values larger then 32 for an index will work in this case so long as the range of values is 0 to 31. So like the letters as mentioned before might be 65-90 or 97-122 but since but we keep remove 32 until we are left with a remainder ranging from 1-26. The remainder isn't how it actually works, but it gives you an idea of the process.

Something to keep in mind if you have no guarantee on the incoming letters it to check if the letter is below 'A' or above 'u'. As the results will always be false anyways.

For example teh following will return a false vowel positive. "!" exclamation point is value 33 and it will provide the same bit value as 'A' or 'a' would.

Cruciate answered 30/1, 2019 at 20:35 Comment(1)
I do understand how bits work but I don't know how you ended up with 2130466 mask. Do the vowels have anything in particular, bitwise?Apoenzyme
R
1

For starters, you are checking if the letter is "not a" OR "not e" OR "not i" etc.

Lets say that the letter is i. Then the letter is not a, so that returns "True". Then the entire statement is True because i != a. I think what you are looking for is to AND the statements together, not OR them.

Once you do this, you need to look at how to increment y and check this again. If the first time you get a vowel, you want to see if the next character is a vowel too, or not. This only checks the character at location y=3.

Ravenna answered 3/10, 2013 at 13:55 Comment(0)
G
0
 String word="Jaemeas";
 String wordT="";
        int y=3;
        char z;
        do{
            z=word.charAt(y);
             if(z!='a'&&z!='e'&&z!='i'&&z!='o'&&z!='u'&&y<word.length()){
                for(int i = 0; i<=y;i++){
                    wordT=wordT+word.charAt(i);
                    }
            break;
            }
           else{
                 y++;
             }

        }while(true);

here is my answer.

Gora answered 3/10, 2013 at 14:9 Comment(1)
Please explain your answer, don’t just provide a block of code (what did you change, why, how does it work).Nymphomania
A
0

I have declared a char[] constant for the VOWELS, then implemented a method that checks whether a char is a vowel or not (returning a boolean value). In my main method, I am declaring a string and converting it to an array of chars, so that I can pass the index of the char array as the parameter of my isVowel method:

public class FindVowelsInString {

        static final char[] VOWELS = {'a', 'e', 'i', 'o', 'u'};

        public static void main(String[] args) {

            String str = "hello";
            char[] array = str.toCharArray();

            //Check with a consonant
            boolean vowelChecker = FindVowelsInString.isVowel(array[0]);
            System.out.println("Is this a character a vowel?" + vowelChecker);

            //Check with a vowel
            boolean vowelChecker2 = FindVowelsInString.isVowel(array[1]);
            System.out.println("Is this a character a vowel?" + vowelChecker2);

        }

        private static boolean isVowel(char vowel) {
            boolean isVowel = false;
            for (int i = 0; i < FindVowelsInString.getVowel().length; i++) {
                if (FindVowelsInString.getVowel()[i] == vowel) {
                    isVowel = true;
                }
            }
            return isVowel;
        }

        public static char[] getVowel() {
            return FindVowelsInString.VOWELS;
        }
    }
Actually answered 25/7, 2017 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.