How do I check if a char is a vowel?

2019-01-08 00:32发布

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.

7条回答
迷人小祖宗
2楼-- · 2019-01-08 01:09

Clean method to check for vowels:

public static boolean isVowel(char c) {
  return "AEIOUaeiou".indexOf(c) != -1;
}
查看更多
神经病院院长
3楼-- · 2019-01-08 01:10

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.

查看更多
\"骚年 ilove
4楼-- · 2019-01-08 01:19

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.

查看更多
淡お忘
5楼-- · 2019-01-08 01:20

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;
        }
    }
查看更多
戒情不戒烟
6楼-- · 2019-01-08 01:26
 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.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-08 01:30

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
查看更多
登录 后发表回答