Count all vowels inputed from keyboard : Scanner « Development Class « Java






Count all vowels inputed from keyboard

  
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++;
    }
    System.out.println("That string contains " + vowelCount + " vowels.");
  }
}

   
  








Related examples in the same category

1.Read int by using Scanner Class
2.Use Scanner to read user input
3.Use Scanner to compute an average of the values
4.Use Scanner to compute an average of the values in a file
5.Use Scanner to read various types of data from a file
6.Use Scanner to compute an average a list of comma-separated values
7.Demonstrate findInLine()
8.use Scanner to read input that contains several different types of data
9.Letting the user decide when to quit
10.Test the input string in the while condition.
11.This program demonstrates console input.
12.Reading double value from console with ScannerReading double value from console with Scanner