217k views
3 votes
Write a class with a constructor that accepts a String object as its argument. The class should have a method that returns the number of vowels in the string, and another method that returns the number of consonants in the string. (Spaces count as neither vowels nor consonants and should be ignored.) Demonstrate the class in a program that performs the following steps:1. The user is asked to enter a string .

2. The program displays the following menu:
1. Count the number of vowels in the string
2. Count the number of consonants in the string
3. Count both the vowels and consonants in the string
4. Enter another string
5. Exit the program

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class VowelsAndConsonantsDemo {

/**

The printMenu methods displays a menu to the user

*/

public static void printMenu() {

System.out.println("Please select an option: ");

System.out.println();

System.out.print("a. Count the number of vowels in the string.\\"

+ "b. Count the number of consonants in the string.\\"

+ "c. Count both the vowels and consonants in the string.\\"

+ "d. Enter another string.\\"

+ "e. Exit the program\\");

}

public static void main(String[] args) {

String input; // to hold the user's input

String option; // to hold the user's input

char choice; // to hold a single character

char exit; // user chooses 'e' to exit the program

char letter; //the Y or N from the user's decision to exit

// create a Scanner object to read keyboard input.

// Scanner keyboard = new Scanner(System.in);

Scanner keyboard;

do {

keyboard = new Scanner(System.in);

// ask user to enter string

System.out.print("Enter a string: ");

input = keyboard.nextLine();

input = input.toLowerCase();

System.out.println();

printMenu();

option = keyboard.nextLine();

choice = option.charAt(0);

VowelsAndConsonants words = new VowelsAndConsonants(input);

switch (choice) {

case 'a':

case 'A':

System.out.println("Number of Vowels: " + words.getVowels());

break;

case 'b':

case 'B':

System.out.println("Number of Consonants: " + words.getConsonants());

break;

case 'c':

case 'C':

System.out.println("Number of Vowels & Consonants: " + words.getConsonants()

+ words.getVowels());

break;

case 'd':

case 'D':

System.out.println("Enter a string: ");

break;

case 'e':

case 'E':

System.exit(0);

break;

default:

System.out.println("You did not enter a valid choice.");

}

//

// keyboard.nextLine(); //consumes the new line character after the choice

// String answer = keyboard.nextLine();

// letter = answer.charAt(0);

} while (true);

}

}

User William Hurst
by
3.7k points