Write a method named isAllVowels that returns whether a String consists entirely
ID: 3557496 • Letter: W
Question
Write a method named isAllVowels that returns whether a String consists entirely of vowels (a, e, i, o, or u, case-insensitively). If every character of the String is a vowel, your method should return true. If any character of the String is a non-vowel, your method should return false. Your method should return true if passed the empty string, since it does not contain any non-vowel characters.
For example, here are some calls to your method and their expected results:
Code:
public static boolean isAllVowels(String s)
{
boolean flag = false;
for (int i = 0; i < s.length(); i++)
{
switch(Character.toLowerCase(s.charAt(i)))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
//case " " :
flag = true;
break;
default:
flag = false;
}
}
return flag;
}
public static void main (String args[])
{
System.out.println(isAllVowels("eIEiO"));
System.out.println(isAllVowels("oink"));
}
Explanation / Answer
public class VowelOrNot
{
public static void main (String args[])
{
System.out.println(isAllVowels("eIEiO"));
System.out.println(isAllVowels("oink"));
}
public static boolean isAllVowels(String s)
{
boolean flag = false;
if(s == "" )
{
return true;
}
for (int i = 0; i < s.length(); i++)
{
char ch = Character.toLowerCase(s.charAt(i));
if(s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u' )
{
flag = true;
}
else
{
flag = false;
break;
}
}
return flag;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.