Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DRIVER
- // Austin Sheidy
- // 4.16.19
- // CH8PC4
- import java.util.Scanner;
- public class AS_WordAnalyzer_Driver {
- public static void main(String[] args) {
- // initialize variables
- int userContinue = 4;
- int vowels = 0;
- int consonants= 0;
- char[] analyze;
- String vowelOut = "are";
- String consonantOut = "are";
- String consonant = "consonants";
- String vowel = "vowels";
- // initialize a scanner for ints and strings
- Scanner keyboard = new Scanner(System.in);
- Scanner keyboardInts = new Scanner(System.in);
- // while loop housing switch statement
- while (userContinue != 5) {
- switch (userContinue) {
- // case 1 is to tell the user how many vowels are in the entered string after cleaning
- case 1:
- System.out.printf("There %s %s %s in that string. \n", vowelOut, vowels, vowel);
- AS_WordAnalyzer.displayMenu();
- if(keyboardInts.hasNextInt())
- userContinue = keyboardInts.nextInt();
- else
- userContinue = 5;
- break;
- // case 2 is to tell the user how many consonants are in the entered string after cleaning
- case 2:
- System.out.printf("There %s %s %s in that string. \n", consonantOut, consonants, consonant);
- AS_WordAnalyzer.displayMenu();
- if(keyboardInts.hasNextInt())
- userContinue = keyboardInts.nextInt();
- else
- userContinue = 5;
- break;
- // case 3 is to tell the user how many vowels and consonants are in the entered string after cleaning
- case 3:
- System.out.printf("There %s %s %s and %s %s in that string. \n", vowelOut, vowels, vowel,
- consonants, consonant);
- AS_WordAnalyzer.displayMenu();
- if(keyboardInts.hasNextInt())
- userContinue = keyboardInts.nextInt();
- else
- userContinue = 5;
- break;
- // case 4 is the default case, gets input, cleans it, then establishes the variables for the rest of
- // the program to use.
- case 4:
- // reset the string outputs
- vowelOut = "are";
- consonantOut = "are";
- consonant = "consonants";
- vowel = "vowels";
- // get input from the user
- System.out.println("Input a string to be analyzed: ");
- String userInput = keyboard.nextLine();
- String userInputUpper = userInput.toUpperCase();
- // clean the string
- StringBuilder userInputNoSpecial = new StringBuilder("");
- for (int i = 0; i < userInputUpper.length(); i++) {
- if (Character.isLetter(userInputUpper.charAt(i))) {
- userInputNoSpecial.append(userInputUpper.charAt(i));
- }
- }
- // create char array
- analyze = userInputNoSpecial.toString().toCharArray();
- vowels = AS_WordAnalyzer.getVowels(analyze);
- consonants = AS_WordAnalyzer.getConsonants(analyze);
- // set the variables for the rest of the program
- if(vowels == 1){
- vowelOut = "is";
- vowel = "vowel";
- }
- if(consonants == 1) {
- consonantOut = "is";
- consonant = "consonant";
- }
- // see what the user wants to do
- AS_WordAnalyzer.displayMenu();
- if(keyboardInts.hasNextInt())
- userContinue = keyboardInts.nextInt();
- else
- userContinue = 5;
- break;
- // default case kills the program if the user enters an int that is not a case
- default:
- userContinue = 5;
- break;
- }
- }
- }
- }
- // CLASS
- // Austin Sheidy
- // 4.16.19
- // CH8PC4
- public class AS_WordAnalyzer {
- private static char[]vowelArray = {'A', 'E', 'I', 'O', 'U'};
- // Check to see if a character is a vowel
- // This method is the equivalent of the word IN from Python
- public static boolean isVowel(char character){
- boolean vowel = false;
- for(int i = 0; i < vowelArray.length; i++){
- if(character == vowelArray[i]){
- vowel = true;
- }
- }
- return vowel;
- }
- // Check for number of vowels
- public static int getVowels(char[] array){
- int count = 0;
- for(int i = 0; i < array.length; i++){
- if(isVowel(array[i]))
- count++;
- }
- return count;
- }
- // Check for number of consonants
- public static int getConsonants(char[] array){
- return (array.length - getVowels(array));
- }
- // check for total number of letters
- public static int getLetters(char[] array){
- return array.length;
- }
- public static void displayMenu(){
- System.out.println("1. Count the number of vowels \n" +
- "2. Count the number of consonants \n" +
- "3. Count both vowels and consonants \n" +
- "4. Enter another string \n" +
- "5. Exit the program");
- }
- }
Add Comment
Please, Sign In to add comment