public class CountCharsA {//naming class public static void main(String[] args) {//main method int linNum = 00;//creating int variable linenumber and setting i t to 00 Scanner in = new Scanner(System.in); // creating a new scanner n amed in while (in.hasNextLine()) { // creating the while loop String input = in.nextLine();//creating a string input a nd setting it to the user's input linNum++;//incrementing linenum variable int vowels = 0;//creating int variable vowels and settin g it to 0 int consonants = 0; //creating int variable consonants a nd setting it to 0 int otherc = 0; //creating int variable otherc and setti ng it to 0 counts(linNum, input); //linnum and input in counts meth od } } static int[] letterCountArray = new int[26]; //creating a static integer array named lettercountarray and creating 26 items in it private static void addLetterCount(char c) { //add letter count method letterCountArray[c - 'a']++; //taking the letter count array, th e char variable c, subtracting it from 'a' and incrementing } private static void zeroLetterCount() { //zerolettercount method for (int i = 0; i < letterCountArray.length; i++) { //for i = i, i less than the length of lettercountarray length, i++ do the following code letterCountArray[i] = 0; //zero out the array } } private static void printLetterCounts() { //printlettercounts method for (int i = 0; i < letterCountArray.length; i++) { if (letterCountArray[i] != 0) { //if the item variable i in lettercountarray does not equal 0 char x = (char) (i + 'a'); //creating char varia ble x and setting it equal to (char) (i+'a') System.out.print(x + "=" + letterCountArray[i] + " "); //printing out char x, lettercountarray variable i, and a blank space } } }
private static int countVowels() { //countvowels method
int vowels = 0; //creating int variable vowels and setting it eq ual to 0 for (int i = 0; i < letterCountArray.length; i++) { //for int i = 0, i less than length of lettercount array, increment i, do the following code char x = (char) (i + 'a'); //creating char variable name d x, and setting it equal to (char) (i+'a') if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') { //seeing if char x is equal to the vowels vowels += letterCountArray[i]; //if it is, add l ettercountarray[i] variable to vowels, and puts the result in vowels variable } } return vowels; //returning the result stored in vowels variable } private static String anyVowels() { //anyvowels method if (countVowels() == 0) { //if countvowels method equals to 0 do the following System.out.print("NO "); //print "NO " return "NO"; //return the string NO } else { //otherwise do this System.out.print("YES "); //print out "YES " return "YES"; //return the string YES } } private static int countConsonants() { //countconsonants method int consonants = 0; //creating int consonants variable and setti ng it equal to 0 for (int i = 0; i < letterCountArray.length; i++) { //for int i = 0, i less than length of letter count array increment i, do the following code char x = (char) (i + 'a'); //create char variable x and set it to (char) (i + 'a) if (x != 'a' && x != 'e' && x != 'i' && x != 'o' && x != 'u') { //check to see if x is a consonant (not equal to any vowels) consonants += letterCountArray[i]; //if it is ad d lettercountarray[i] variable to consonants and put results in consonants } } return consonants; //return result stored in consonants variable } private static void counts(int linNum, String s) { //counts method zeroLetterCount(); //calling zerolettercount method int otherc = 0; //creating int otherc and setting it equal to 0 for (int i = 0; i < s.length(); i++) { //for int i = 0, i less t han length of string s variable, increment i do the following code char c = s.charAt(i); //creating char c variable and set ting it equal to s.charAt(i) if (c >= 'a' && c <= 'z') { //checking to see if a char
c is greater or equal to a and less than or equal to z
addLetterCount(c); //return addlettercount metho d is variable c } else { //if c is not greater than a or less than z do otherc++; //increment otherc variable } } System.out.print("Line "); //print line System.out.printf("%02d", linNum); //print linNum variable forma tted System.out.print(": "); //print : and a space printLetterCounts(); //calling printlettercounts method anyVowels(); //calling any vowels method System.out.print("vowels=" + countVowels()); //printing out vowe ls and countvowels System.out.print(" " + "consonants=" + countConsonants()); //pri nting out consonants and countconsonants System.out.print(" " + "others=" + otherc); //printing out other c System.out.println(); } }