Mega Code Archive

 
Categories / Java Tutorial / Data Type
 

Using the Collection-Based for Loop with a String

public class MainClass {   public static void main(String[] arg) {     String phrase = "The quick brown fox jumped over the lazy dog.";     int vowels = 0;     for(char ch : phrase.toCharArray()) {       ch = Character.toLowerCase(ch);       if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {         ++vowels;       }     }     System.out.println("The phrase contains " + vowels + " vowels.");         } } The phrase contains 12 vowels.