Problem:
Write a class Vowels that reads a one-line statement from the user, then determines and prints how many of each vowel (A/a, E/e, 1/i, 0/o, and U/u) and many non-vowel character appear in the entire statement. You need a separate counter for each vowel and one counter for the non-vowel characters. You need to use the switch statement to determine the vowel characters.
Output:
Please enter a statement: Hello my name is Amer
The number of "A"s (lower and upper case) is: 2
The number of "E"s (lower and upper case) is: 6
The number of "I"s (lower and upper case) is: 1
The number of "0"s (lower and upper case) is: 1
The number of "U"s (lower and upper case) is: 0
The number of non-vowels is: 7
The number of "A"s (lower and upper case) is: 2
The number of "E"s (lower and upper case) is: 6
The number of "I"s (lower and upper case) is: 1
The number of "0"s (lower and upper case) is: 1
The number of "U"s (lower and upper case) is: 0
The number of non-vowels is: 7
Solution:
import java.util.Scanner; public class Problem2 { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.println("Please enter a statement:"); String statement = scan.nextLine(); int i = 0; int vowelAcount = 0; int vowelEcount = 0; int vowelIcount = 0; int vowelOcount = 0; int vowelUcount = 0; int nonvowel= 0; int spacescount = 0; while (i < statement.length()) { char chararcter = statement.charAt(i++); switch (chararcter) { case 'A': case 'a': vowelAcount++; break; case 'E': case 'e': vowelEcount++; break; case 'I': case 'i': vowelIcount++; break; case 'O': case 'o': vowelOcount++; break; case 'U': case 'u': vowelUcount++; break; case ' ': spacescount++; break; default: nonvowel++; break; } } System.out.println("The number of As (lower and upper case) is : "+ vowelAcount); System.out.println("The number of Es (lower and upper case) is : "+ vowelEcount); System.out.println("The number of Is (lower and upper case) is : "+ vowelIcount); System.out.println("The number of Os (lower and upper case) is : "+ vowelOcount); System.out.println("The number of Us (lower and upper case) is : "+ vowelUcount); System.out.println("The number of non-vowels is : " + nonvowel); } }
No comments :
Post a Comment