Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
im currently stock in my java program?
this is what i have to do
Write a recursive method named extractNonVowels. This method shall
take a String s as a parameter and returns a String without any non-
vowel. For example: If the String s = ``Hello world", after calling
extractVowels, the String shall be ``eoo"
this is what i have
public class FIndVowels
{
public static void main(String [] args)
{
System.out.println(extracNontVowels("bananas")
}
public static String extractNonVowels(String s)
{
if(s.length() <= 0)
{
return s;
}
else
{
if(s.charAt(0)=='a'||s.charAt(0)=='A')
return extractNonVowels(s.substring(1)) + s.charAt(0);
if(s.charAt(0)=='o'||s.charAt(0)=='O')
return extractNonVowels(s.substring(1)) + s.charAt(0);
if(s.charAt(0)=='e'||s.charAt(0)=='E')
return extractNonVowels(s.substring(1))+ s.charAt(0);
if(s.charAt(0)=='i'||s.charAt(0)=='I')
return extractNonVowels(s.substring(1)) + s.charAt(0);
if(s.charAt(0)=='u'||s.charAt(0)=='U')
return extractNonVowels(s.substring(1)) + s.charAt(0);
}
return extractNonVowels(s.substring(1));
}
}
im currently stuck
this is what the fifth line says it got cut off when i posted it
System.out.println(extractNonVowels("bananas")
2 Answers
- davidLv 76 years agoFavorite Answer
It's OK, I can imagine what's on that line. (Yahoo will cut things off if they don't contain a space.)
You don't say what the problem is. Your program appears to mostly work, except it's printing in reverse order.
Change this:
return extractNonVowels(s.substring(1)) + s.charAt(0)
to this:
return s.charAt(0) + extractNonVowels(s.substring(1))
and it should be good.
- JohnLv 76 years ago
public class Program {
public static void main(String[] args) {
String result = extractNonVowels( "Hello world");
System.out.println(result);
}
private static String extractNonVowels( String value) {
String pattern = "(?i)[^aeiou]";
return value.replaceAll(pattern, "");
}
}