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

  • SEARCH
  • Home
  • 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 8712077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:02:07+00:00 2026-06-13T05:02:07+00:00

I am supposed to write a simple java application that counts each vowel in

  • 0

I am supposed to write a simple java application that counts each vowel in a string entered by the user and outputs the number of time each vowel occurs.

I don’t understand why my code is checking each individual word in my string. I am getting the right amount of vowels for each word. Here is what I have:

import java.util.Scanner;

public class VowelAnalyst
     {
//
//

public static void main (String[] args)
{
String userString;
int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
char vowels;

Scanner scan = new Scanner (System.in);

System.out.println ("enter string:");
userString = scan.nextLine();

for (int count = 0; count < userString.length(); count++)
{

vowels = userString.charAt(count);

    switch (vowels)
    {
    case 'a':
        aCount++;
        break;

    case 'e':
        eCount++;
        break;

    case 'i':
        iCount++;
        break;

    case 'o':
        oCount++;
        break;

    case 'u':
        uCount++;
        break;

    default:
        System.out.println ("Please enter valid string.");

    }

            System.out.println ("a: " +aCount);
            System.out.println ("e: " +eCount);
            System.out.println ("i: " +iCount);
            System.out.println ("o: " +oCount);
            System.out.println ("u: " +uCount);
        }
    }
}
  • 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-13T05:02:08+00:00Added an answer on June 13, 2026 at 5:02 am

    May be you should move your below print statements out of your for loop, else they will print count after every character compared: –

    System.out.println ("a: " +aCount);
    System.out.println ("e: " +eCount);
    System.out.println ("i: " +iCount);
    System.out.println ("o: " +oCount);
    System.out.println ("u: " +uCount);
    

    UPDATE: –

    Although the way you are doing is not a bad way, but you would be better if you maintain a Map<Character, Integer> to store the count of each Vowel. You need to initialize your Map with an initial count of 0 for each character, and then on each character read, just increment the count, if match is found in Map.

    Here’s a sample snippet: –

    // This is `double-braces` initialization. 
    // You can rather initialize your Map in a way you are comfortable with
    Map<Character, Integer> vowels = new HashMap<Character, Integer>() {
        {
            put('a', 0);
            put('e', 0);
            put('i', 0);
            put('o', 0);
            put('u', 0);
        }
    }; // Note the semi-colon here
    

    And then your code of reading each character from string in for loop: –

    for (int count = 0; count < userString.length(); count++)
    {
        char ch = userString.charAt(count);
        ch = Character.toLowerCase(ch);
    
        if (vowels.containsKey(ch)) {
            vowels.put(ch, vowels.get(ch) + 1); 
        }
    }
    
    System.out.println(vowels);  // Will print each vowels with respective count
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple color class that's supposed to be as versatile
I'm supposed to write a wrapper function to MD5 hashing that restricts the input
I am supposed to write a program that gets some PNG images from the
I'm trying to write a very simple number guessing game (code is below). After
I have a simple problem that says: A password for xyz corporation is supposed
I want to write a simple filtering pass-through filesystem for linux. It's supposed to
I'm trying to write a simple regex for my java program to count syllables,
I found this piece of Java code at Wikipedia that is supposed to shuffle
I am trying to write a simple mail client in Java with sockets. But
I'm working on an application that's supposed to read and process flat files. These

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.