Java examples for Language Basics:String
Uses the charAt method to count the number of vowels in a string entered by the user:
import java.util.Scanner; public class CountVowels { static Scanner sc = new Scanner(System.in); public static void main(String[] args){ System.out.print("Enter a string: "); String s = sc.nextLine(); int vowelCount = 0; for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u') ) vowelCount++;/*from w w w.jav a2s . c o m*/ } System.out.println("That string contains " + vowelCount + " vowels."); } }