Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:50:39+00:00 2026-06-03T21:50:39+00:00

I changed my code as Dan suggested i can compile the program now, however,

  • 0

I changed my code as Dan suggested i can compile the program now, however, whatever the input is, the result is always 2. i put the second part of this program below the new code. please help.

Here’s the NEW code.

    public class VowelCons 
        {
    private final String str;
    private final int totalConsonants;
    private final int totalVowels;

        public VowelCons(final String s) 
    {
             this.str = s;
                int totalConsonants = 0;
                int totalVowels = 0;
                if (null != s) 
        {
                    for (final char c : s.toCharArray()) 
            {
                                switch (c) 
                    {
                                        case 'A':
                                        case 'a':
                                        case 'E':
                                        case 'e':
                                        case 'I':
                                        case 'i':
                                        case 'O':
                                        case 'o':
                                        case 'U':
                                        case 'u':

                        totalVowels++;
                                            break;

                        default:

                        if (Character.isLetter(c)) 
                        {
                                                    totalConsonants++;
                                            }
                                            break;
                                    }
                        }
                }
            this.totalConsonants = totalConsonants;
            this.totalVowels = totalVowels;
        }


    public String getString() 
    {
             return str;
    }

    public int getNumConsonants() 
    {
                return this.totalConsonants;
    }

    public int getNumVowels() 
    {
                return this.totalConsonants;
    }
}

there’s another part of this program which gets the user’s input and passes it to this class.
Here’s the code. [this part cannot be changed according to the regulations]

    import java.util.Scanner;

    public class VowelConsCounter
    {
        public static void main(String[] args)
        {
         String input;        // User input
         char selection;      // Menu selection

         Scanner keyboard = new Scanner(System.in);

         System.out.print("Enter a string: ");
         input = keyboard.nextLine();

         VowelCons vc = new VowelCons(input);

        do
        {
          selection = getMenuSelection();

          switch(Character.toLowerCase(selection))
          {
            case 'a' :  System.out.println("\nNumber of vowels: " +
                        vc.getNumVowels());
                        break;
            case 'b' :  System.out.println("\nNumber of consonants: " +
                        vc.getNumConsonants());
                        break;
            case 'c' :  System.out.println("\nNumber of vowels: " +
                        vc.getNumVowels());
                        System.out.println("Number of consonants: " +
                        vc.getNumConsonants());
                        break;
            case 'd' :  System.out.print("Enter a string: ");
                        input = keyboard.nextLine();
                        vc = new VowelCons(input);
         }

      } while (Character.toLowerCase(selection) != 'e');

   }

   public static char getMenuSelection()
   {
      String input;     
      char selection;   

      Scanner keyboard = new Scanner(System.in);

      System.out.println("a) Count the number of vowels in the string.");
      System.out.println("b) Count the number of consonants in the string.");
      System.out.println("c) Count both the vowels and consonants in the string.");
      System.out.println("d) Enter another string.");
      System.out.println("e) Exit the program.");

      input = keyboard.nextLine();
      selection = input.charAt(0);

      while (Character.toLowerCase(selection) < 'a' || Character.toLowerCase(selection) > 'e')
      {
         System.out.print("Only enter a, b, c, d, or e: ");
         input = keyboard.nextLine();
         selection = input.charAt(0);
      }

      return selection;
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:50:40+00:00Added an answer on June 3, 2026 at 9:50 pm

    You’re getting a NullPointerException because you are not initializing the instance variable result.

    I would recommend using the following:

    public class VowelCons {
        private final String str;
        private final int totalConsonants;
        private final int totalVowels;
    
        public VowelCons(final String s) {
            this.str = s;
            int totalConsonants = 0;
            int totalVowels = 0;
            if (null != s) {
                for (final char c : s.toCharArray()) {
                    switch (c) {
                        case 'A':
                        case 'a':
                        case 'E':
                        case 'e':
                        case 'I':
                        case 'i':
                        case 'O':
                        case 'o':
                        case 'U':
                        case 'u':
                            totalVowels++;
                            break;
                        default:
                            if (Character.isAlphabetic(c)) {
                                totalConsonants++;
                            }
                            break;
                    }
                }
            }
            this.totalConsonants = totalConsonants;
            this.totalVowels = totalVowels;
        }
    
        public String getString() {
            return str;
        }
    
        public int getTotalConsonants() {
            return this.totalConsonants;
        }
    
        public int getTotalVowels() {
            return this.totalConsonants;
        }
    
        public String toString() {
            return (null == str ? "" : str) + " [consonants=" + totalConsonants + ", vowels=" + totalVowels + "]";
        }
    
        public static void main(final String[] args) {
            for (final String arg : args) {
                final VowelCons vc = new VowelCons(arg);
                System.out.println(vc.toString());
            }
        }
    }
    

    This would, for example, output:

    $ java VowelCons foo BaR "Lady GODIVA"
    foo [consonants=1, vowels=2]
    BaR [consonants=2, vowels=1]
    Lady GODIVA [consonants=6, vowels=4]
    

    Here are some points this example should help you learn:

    1. Local variables may hide instance variables (see [1] and [2]).
    2. Use this to reference instance variables (see [1]). You should always reference instance variables using this, which prevents accidentally hiding it when you make future code changes and allows IDEs to provide context sensitive suggestions that only contain instance members.
    3. Handle null Strings passed to the constructor.
    4. Use switch to simplify the logic and reduce redundant code in if-else logic.
    5. Check for lower-case and upper-case vowels.
    6. Ignore non-alphabetic characters in the vowel/consonant counts.
    7. Implement a custom toString().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have changed a lot of things in a java code but now I
I changed the code shown below into ARC compatible. I just changed it as
I have the following code (changed object names, so syntax/spelling errors ignore). public class
This code used to work. Then, maybe I changed something, somewhere (or if I
I wrote some code today and It was changed by another developer who said
The other day this code was working. I changed some things and re-ran it
I've changed from Git to SVN the folders which contains the project's code, but
I am using generic code (from the iOS Fireworks demo) in a slightly changed
I have following code which works for radio buttons but need to be changed
When I debug code below, I see that span is changed by mapkit from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.