Vowel Counter
The Code
The code for the fourth class of Java is as follows:
package vowelcounter;
import java.util.Scanner;
public class VowelCounter {
public static void main(String[] args) {
/*
Program to count the vowels and consonants in a word.
Students will use this and incorporate checks for other characters.
*/
int x = 0 ;
int no_vowels = 0, no_consonants = 0;
String fullname = " ";
Scanner o = new Scanner(System.in);
System.out.println("Type your name ==>");
fullname = o.nextLine().toUpperCase();
// Use a do...while to loop through the word
do {
Character check = fullname.charAt(x);
// if ...else construct to evaluate letter
if (check == 'A') {
no_vowels++;
}else if (check == 'E') {
no_vowels++;
}
else if (check == 'I') {
no_vowels++;
}
else if (check == 'O') {
no_vowels++;
}
else if (check == 'U') {
no_vowels++;
}
else {
no_consonants++; //Assuming for now, only letters are inserted
// so if not a vowel, letter must be a consonant
}
x++;
} while (x< fullname.length());
System.out.println("Your name contains ");
System.out.println(no_vowels+" vowels and " +no_consonants+ " consonants");
} // End of main
} // End of class