+ 2
java help
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; } I have this method. But I don't know how to write another methods that returns whether a string consists entirely of vowels and in this methods I need to call method above. Thanks you so much for your help:)
4 Respostas
+ 21
// Try this
public class Program
{
public static void main(String[] args) {
String a="SoloLearn";
int b=0;
for(int i=0;i<a.length();i++)
{
if(isVowel(""+a.charAt(i)))
{
b++;
}
}
if(b>=1)
{
System.out.println("true");
}
else
System.out.println("false");
}
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;
}
}
+ 16
● run a loop over each character of that String
● using .charAt() method[take out each char] & compare it with 'a' , 'e' , 'i' , 'o' , 'u'
● using || opeartor [as we need any one of them] , if any one of them matches returns true else false
+ 1
you can use switch statement also
+ 1
In case that using your method isVowel is not mandatory, you can use regex's and the task will be quite simple.
public static boolean allVowels(String x) {
return x.toLowerCase().matches( "^[aeiou]+quot; );
}
And if we want a perfect code, both your method and mine should have a check at the beginning of null input in order to avoid a NullPointerException.