I have having problems figuring out how to get this code to run properly. I cannot seem to get my do-while loop in the main method to repeat. Also, I am having issues with my getConsonants method. It seems to always count 1 less than there actually is. Lastly, not sure how I should write the total characters count. The way I have it now, I get outrageous numbers. Any help would be greatly appreciated.

Here's the problem:
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.

Demontrate 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:
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
3. The program performs the operation selected by the user and repeats until the user selects e, to exit the program.

import java.util.StringTokenizer;
/**
   FileName: VowelsAndConsonants.java
   
   The VowelsAndConsonants class 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.
*/

public class VowelsAndConsonants
{
   private String consonants = "bcdfghjklmnpqrstvwxz" +
                               "BCDFGHJKLMNPQRSTVWXZ";
   private String string;
   int i = 0;
   int a = 0;
   int vowels = 0;
   int con = 0;

   /**
      Constructor
      @param in The input to analyze.
   */

   public VowelsAndConsonants(String str)
   {
      string = str;
   }

   /**
      The getVowels method returns the 
      number of vowels in the string.
      @return The number of vowels in the string.
   */

   public int getVowels(String str)
   {
      a = str.length();

      for (i=0; i<a; i++)
      {
         if(str.charAt(i) == 'a' || str.charAt(i) == 'e' ||
            str.charAt(i) == 'i' || str.charAt(i) == 'o' ||
         str.charAt(i) == 'u')
         vowels++;
      }
      return vowels;
   }

   /**
      The getConsonants method returns the 
      number of consonants in the string.
      @return The number of consonants in the string.
   */

   public int getConsonants(String str)
   {
      StringTokenizer tok = new StringTokenizer(str);

      while (tok.hasMoreTokens())
      {
         String word = tok.nextToken();

         for (int b=0; b<word.length(); b++)
         {
            if (consonants.indexOf(word.charAt(b)) != -1)
            con++;
         }
      }
      return con;
   }
}
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.\n" +
                       "b. Count the number of consonants in the string.\n" +
                       "c. Count both the vowels and consonants in the string.\n" +
                       "d. Enter another string.\n" +
                       "e. Exit the program\n");

   }

   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);
      
      do
      {
         // 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(input));
               break;
            case 'b':
            case 'B':
               System.out.println("Number of Consonants: "+ words.getConsonants(input));     
               break;
            case 'c':
            case 'C':
               System.out.println("Number of Vowels & Consonants: "+ words.getConsonants(input) +
                                   words.getVowels(input));
               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 (letter != 'E' && letter != 'e');
   }
}

Your class should looks lik:

/**
FileName: VowelsAndConsonants.java

The VowelsAndConsonants class 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.
 */
public class VowelsAndConsonants {

    private String string;
    
    int vowels = 0;
    int con = 0;

    /**
    Constructor
    @param in The input to analyze.
     */
    public VowelsAndConsonants(String str) {
        this.string = str;
        this.parsse();
    }

    private void parsse() {
        
         int a = string.length();
        for (int i = 0; i < a; i++) {
            if (this.isVowel(string.charAt(i))) {
                vowels++;
            }else{
                if (this.isConsonant(string.charAt(i))) {
                con++;
                }
            }
        }
    }

    /**
    The getVowels method returns the
    number of vowels in the string.
    @return The number of vowels in the string.
     */
    public int getVowels() {
        return vowels;
    }

    private boolean isVowel(char c) {
        return (c == 'a' || c == 'e'
                || c == 'i' || c == 'o'
                || c == 'u');
    }

    private boolean isConsonant(char c) {
        return (((c >= 'a' && c <= 'z') || (c >= 'A' && c >= 'Z')) && !isVowel(c));
    }

    /**
    The getConsonants method returns the
    number of consonants in the string.
    @return The number of consonants in the string.
     */
    public int getConsonants() {
        return con;
    }
}

and the client teste:

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.\n"
                + "b. Count the number of consonants in the string.\n"
                + "c. Count both the vowels and consonants in the string.\n"
                + "d. Enter another string.\n"
                + "e. Exit the program\n");

    }

    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);
    }
}

Hope it helps.

Thank you Moutanna. Works like a charm!

Happy to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.