English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To recalculate the number of occurrences of vowels in a string, use Java's Map utility to calculate the number of occurrences of each character in the string. Place each vowel in the map as a key, and set the initial value of each key to1Now, compare each character with the keys in the map. If the character matches the key, increase the corresponding value.1.
public class OccurenceVowel { public static void main(String[] args) { String str = "AEAIOG"; LinkedHashMap<Character, Integer> hMap = new LinkedHashMap(); hMap.put('A', 0); hMap.put('E', 0); hMap.put('I', 0); hMap.put('O', 0); hMap.put('U', 0); for (int i = 0; i <= str.length() - 1; i++) { if (hMap.containsKey(str.charAt(i))) { int count = hMap.get(str.charAt(i)); hMap.put(str.charAt(i), ++count); } } System.out.println(hMap); } }
Output Result
{A=2, E=1, I=1, O=1, U=0