counts the vowels when you write a statement - Java Algorithm

Java examples for Algorithm:String

Description

counts the vowels when you write a statement

Demo Code


import java.util.Scanner;

public class CountVowels {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int vowel = 0;
    String sentence;//from  w ww.  j a v  a2  s .  c o m

    System.out.println("Enter sentence: ");
    sentence = scan.nextLine();

    for (int i = 1; i < sentence.length(); i++) {
      if (sentence.charAt(i) == 'a')
        vowel++;
      else if (sentence.charAt(i) == 'e')
        vowel++;
      else if (sentence.charAt(i) == 'i')
        vowel++;
      else if (sentence.charAt(i) == 'o')
        vowel++;
      else if (sentence.charAt(i) == 'u')
        vowel++;

    }
    System.out.println("The number of vowels in java programming assignment is: " + vowel + ".");

  }

}

Related Tutorials