Jump to content

counting vowels in program

- - - - -

  • Please log in to reply
9 replies to this topic

#1
toppbug

toppbug

    Newbie

  • Members
  • Pip
  • 4 posts
Hello! I am new here - this is my first post.

I am writing a program where the user inputs a sentence, then the program counts the total vowels, letters, and characters.

I have everything done but not only do I have to count the total vowels, I have to display each vowel with the total number in the sentence.
This is my code - I have 3 different ways I came up with to count the total number of vowels in the sentence. I'm just not sure which will be the easiest to use in order to count each vowel letter itself.

public class CountVowels {


    public static void main(String[] args) {


		String input;

		char ch;


		int letA = 0, letE = 0, letI = 0, letO = 0, letU = 0;


		input = JOptionPane.showInputDialog("Input a sentence.");

		int length = input.length();


		char[] characters = input.toCharArray();


		int vowelCount = 0;



			for (int i = 0; i < characters.length; i++)

 			{

     		ch = input.charAt(i);


      	if (    (ch == 'A')   ||  (ch == 'a')

            ||  (ch == 'E')   ||  (ch == 'e')

            ||  (ch == 'I')   ||  (ch == 'i')

            ||  (ch == 'O')   ||  (ch == 'o')

            ||  (ch == 'U')   ||  (ch == 'u')  )

             vowelCount++;


 			}

 		}


		int letters = 0;

		for (int i = 0; i < characters.length; i++)

		{

			if (Character.isLetter(characters[i]))

				letters++;

		}



  		System.out.println(input);

		System.out.println("   ");

  		System.out.println("Vowel\t\tNumber");

		**** This is where I have to list each letter with the number	****

System.out.println("The total number of vowels in the sentence: " + vowelCount);

  		System.out.println("The total number of letters in the sentence: " + letters);

  		System.out.println("The total number of characters in the sentence: " + length);

  }


}




This is the second way I could use to count vowels
int vowels = 0;

 		for (int i = 0; i < characters.length; i++){

 			if (characters[i] == 'a' || characters[i] == 'e' || characters[i] == 'i' || characters[i] == 'o' || characters[i] == 'u')

 				vowels++;


And finally, this is the third way I have..
for (char c : input.toCharArray())

		{

			switch(c){


				case 'A':

				case 'a':

					break;


				case 'E':

				case 'e':

					break;


				case 'I':

				case 'i':

					break;


				case 'O':

				case 'o':

					break;


				case 'U':

				case 'u':

					break;


				default: vowelCount++;


		}

    }



Can anyone tell me which will be the best one to use and how I go about counting each vowel letter? I'm assuming I will use a for loop but I don't really know how to go about doing that with this type of program and I don't know where I would place it if that is what I use.

Thanks in advance for any help!

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
They really seem the same to me. There's no special way I know of to count vowels.
I like this version, just be sure you convert your string to lower case before you do this.

int vowels = 0;

    for (int i = 0; i < characters.length; i++){

 	if (characters[i] == 'a' || characters[i] == 'e' || characters[i] == 'i' || characters[i] == 'o' || characters[i] == 'u')

 		vowels++;



#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
There is another way, but your teacher might not like it cause it's too simple :P

String input = "aabbbccad";
int countA = input.replaceAll("[^a]", "").length();
int countB = input.replaceAll("[^b]", "").length();
etc..

I basically replace everythign that's not "a" , or not "b" with nothing (""), so only the a's and b's remain. Then just use length to count them and it's done ^^

#4
toppbug

toppbug

    Newbie

  • Members
  • Pip
  • 4 posts
I guess I wasn't clear on my problem.

I have everything done, but I need to display each vowel (A,E,I,O,U) with the number of each particular vowel in the sentence the user inputs.

I'm not sure which count vowel method i would use, or how I would go about getting each letter listed.

Anyone have any suggestions?

#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Create an int array with 26 places, 1 place for each character in the alphabet.
Convert the String to lowercase.

The ascii value of 'a' is 97. The ascii value of 'z' is 97 + 26 = 122.
You can loop through the away, use charAt to receive your char.
Subtract this char value from 'a'. This value, is the index of the array you'll be using. Increment the value at this index.
Repeat for each char in the String

Example:


String string = "aabbCCddeeff";

string = convert to lower case;

int[] alphabet = ... ;

for( each char in string )

    char temp = get char from string;

    index = temp - 'a';

    alphabet[index]++;


So in this example, the first iteration through the loop yields:
temp = 'a';
index = 'a' - 'a' = 97 - 97 = 0;
alphabet[0]++; // therefore alphabet[0] = 1 now

Next iteration
temp = 'a';
index = 'a' - 'a' = 97-97 = 0;
alphabet[0]++// therefore alphabet[0] = 2 now

...

#6
toppbug

toppbug

    Newbie

  • Members
  • Pip
  • 4 posts
Since I only need to output the vowels do I still need to go through the whole alphabet?

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

toppbug said:

Since I only need to output the vowels do I still need to go through the whole alphabet?
\

This process saves the amount of occurrences of each letter. You can save JUST the vowels, but you'll have to add more to the program like:
if( index == a_index || index == e_index || index == i_index || .. etc )
.
I'd keep it the way it is. You might save a bit of memory, but not much at all.

#8
toppbug

toppbug

    Newbie

  • Members
  • Pip
  • 4 posts
Ok, so something like this:

String string = "AEIOU";  **CAN I PUT THIS SINCE I ONLY NEED VOWELS?

string = convert to lower case;

int[] alphabet = ... ; **WHAT WOULD GO HERE??

for( each char in string )

    char temp = get char from string;

    index = temp - 'a';  

    alphabet[index]++;


#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
alphabet will hold the occurrence of each letter in the alphabet. So... however many letters are in the alphabet should be the size of that array.

#10
Eieio

Eieio

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
The code below is wrong. This would count to see how many consonants you have. You need to add to the vowel count before those breaks and then leave the default blank.

for (char c : input.toCharArray())

		{

			switch(c){


				case 'A':

				case 'a':

					break;


				case 'E':

				case 'e':

					break;


				case 'I':

				case 'i':

					break;


				case 'O':

				case 'o':

					break;


				case 'U':

				case 'u':

					break;


				default: vowelCount++;


		}

    }

So it should look like this:

for (char c : input.toCharArray())

		{

			switch(c){


				case 'A':

				case 'a':


				case 'E':

				case 'e':


				case 'I':

				case 'i':


				case 'O':

				case 'o':


				case 'U':

				case 'u':

                                        vowelCount++;

					break;


				default: // do nothing


		}

    }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users