English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Program to Accept a String Containing All Vowels

In order to find that the given string contains all vowels, we must first convert the given string into an array of characters so that we can simplify the comparison of each character in the given string.

After that, put each character into a hash map so that we can check if the map created from the given string contains all the vowels. We use hash map here because there is no specific method in the character array class to check if it contains all vowels. The only way to check is to iterate through the entire array and compare each character with each vowel, which is incorrect and the correct method, just like the String class, so we convert the string to a hash map to check if it contains vowels.

Now create a character array list and include all vowels. The remaining steps are to check if the hash map contains the list? By using all the methods of the Map interface.

If the hash map contains the vowel list, then we can accept the given string, otherwise we cannot.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class StringContainVowels {
   public static void main(String[] args) {
      String str1 = "aeiou";
      ArrayList<Character> vowelList = new ArrayList<>(Arrays.asList('a','e','i','o','u'));
      char[] ch1 = str1.toCharArray();
      HashMap<Character,Character> hMap = new HashMap();
      for(char ch : ch2) {
         hMap.put(ch,'g');
      }
      if(hMap.keySet().containsAll(vowelList)) {
         System.out.println("String is accepted");
      } else {
         System.out.println("String is not accepted");
      }
   }
}

Output Result

String is accepted

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class StringContainVowels {
   public static void main(String[] args) {
      String str1 = "aeufgfg";
      ArrayList<Character> vowelList = new ArrayList<>(Arrays.asList('a','e','i','o','u'));
      char[] ch1 = str1.toCharArray();
      HashMap<Character,Character> hMap = new HashMap();
      for(char ch : ch2) {
         hMap.put(ch,'g');
      }
      if(hMap.keySet().containsAll(vowelList)) {
         System.out.println("String is accepted");
      } else {
         System.out.println("String is not accepted");
      }
   }
}

Output Result

String is not accepted