user2179615
user2179615

Reputation: 127

Returning vowels

I am a beginner in Java, and I am doing my practice on practiceit.

But I got stumbled over this question.

Write a method named isVowel that returns whether a String is a vowel (a single-letter string containing a, e, i, o, or u, case-insensitively).

public static boolean isVowel(String word){

  
   for(int i=0;i<word.length();i++){
  

    char vowels=word.charAt(i);
        if(vowels== 'a'|| vowels =='e' || vowels=='i'|| vowels == 'o' ||  vowels == 'u'|| vowels== 'A'|| vowels =='E' || vowels=='I'|| vowels == 'O' ||  vowels == 'U' ){
           return true;
        } 
       
   }
    return false;
}

This code works but when i test it for "hello". It no longer works. I understand it is because the condition is char so it loops one by one and not the word as a whole.But i cant figure out.Will appreciate if someone will give me hints instead of answer.

Upvotes: 0

Views: 12094

Answers (5)

Bilal Khalid
Bilal Khalid

Reputation: 11

The below method worked for me.

public static boolean isVowel(String str) {
    if (str.equalsIgnoreCase("a")) {
        return true;
    } else if (str.equalsIgnoreCase("e")) {
        return true;
    } else if (str.equalsIgnoreCase("i")) {
        return true;
    } else if (str.equalsIgnoreCase("o")) {
        return true;
    } else if(str.equalsIgnoreCase("u")){
        return true;
    } else {
        return false;
    }
}

Upvotes: 0

Rahul Kumar
Rahul Kumar

Reputation: 11

here's the solution you looking for

public static boolean isVowel(String x){

    String y=x.toLowerCase();
    if(y.equals("a"))
        return true;
    else if(y.equals("e"))
        return true;
    else if(y.equals("i"))
        return true;
    else if(y.equals("o"))
        return true;
    else if(y.equals("u"))
        return true;
    else 
        return false;

 }

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Given your definition of the function, a single letter that is a vowel, this should work.

public class StackOverflowAnswer {

    public static void main(String[] args) {
        System.out.println(isVowel("a")); //Returns true
        System.out.println(isVowel("b")); //Returns false
        System.out.println(isVowel("Obama")); //Returns false
        System.out.println(isVowel("ae")); //Returns false, not single letter

    }

    public static boolean isVowel(String word){

           String[] vowels = {"a","e","i","0","u"};

           return Arrays.asList(vowels).contains(word.toLowerCase());
        }
}

Upvotes: 0

Maggie
Maggie

Reputation: 8081

If you just need to check whether your string IS a vowel, instead of CONTAINS a vowel, you could write something like:

public static boolean isVowel(String word){
    if (word.length()!=1) return false;
    char vowels=word.toLowerCase().charAt(0);
    if(vowels== 'a'|| vowels =='e' || vowels=='i'|| vowels == 'o' ||  vowels == 'u'){
       return true;
    } 
    return false;
}

Upvotes: 0

duffymo
duffymo

Reputation: 308743

Your question is quite confusing. A String might have a vowel, but the only proper English "word" with single letter is "I".

This method should be named "hasVowel" or "containsVowel". In that case, looping over each letter and returning true if it has a vowel makes sense.

Upvotes: 0

Related Questions