Mast3rCh1ef
Mast3rCh1ef

Reputation: 23

java Vowel Counter Using Switch Method

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

Upvotes: 2

Views: 16960

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213321

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

Upvotes: 3

Adrian Adamczyk
Adrian Adamczyk

Reputation: 3080

Do you guys really have to create a map to store some characters?

public static void main(String[] args)
{
    char[] vowels = "aeiouAEIOU".toCharArray();
    String text = "I am a Java programmer";
    int[] vowelsCount = new int[vowels.length];

    for (char textChar: text.toCharArray())
    {
        for (int i = 0; i < vowels.length; i++)
        {
            char vowel = vowels[i];

            if (vowel == textChar)
            {
                vowelsCount[i]++;
                break;
            }
        }
    }

    for (int i = 0; i < vowelsCount.length; i++)
    {
        System.out.println(vowels[i] + " - " + vowelsCount[i]);
    }
}

Output:

a - 5
e - 1
i - 0
o - 1
u - 0
A - 0
E - 0
I - 1
O - 0
U - 0

Upvotes: 0

Related Questions