user3293092
user3293092

Reputation: 1

How to create a program that uses an array to count vowels?

I am having some trouble creating a program that uses an array to count vowels in names entered by a user. The user should be able to enter up to 1000 names or say "Done" to end the program. Once the user gets to 1000 names or says done, it is supposed to display the total amount of vowels in each name combined. Here is what I have so far:

import java.util.Scanner;
import java.lang.String;
import java.lang.Math;

public class Countvowels
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        final int LOW ='A';
        final int HIGH = 'Z';
        int[] letterCounts = new int[HIGH-LOW+1];
        String[] word = new String[1000];
        char[] wordLetter;
        int offset;
        System.out.println("Enter a name: ");
        for(int letter = 0; letter < wordLetter.length; letter++){
            word[letter] = input.nextLine();
            wordLetter = word.toCharArray();

        }
    }
}

Help is much appreciated!

Upvotes: 0

Views: 1633

Answers (3)

aliteralmind
aliteralmind

Reputation: 20163

Three errors I see:

You have the array-of-characters

char[] wordLetter;

in which the vowels will go, yet you're using it as the for-loop termination. There's nothing in the array yet--there is no array at all, just a marker in memory where it will be created--so you are comparing letter against nothingness!

for(int letter = 0; letter < wordLetter.length; letter++){

It should be

for(int letter = 0; letter < [some_number_here]; letter++){

In the for-loop, you are attempting to change the entire array of words to a character array, which makes no sense. Naming the array of words word is confusing you. Try aWords or something.

          wordLetter = word.toCharArray();

Fix:

          wordLetter = word[letter].toCharArray();

And letter is another bad choice of variable names. Try iIndex.

I hope this helps!

Upvotes: 0

Timmetje
Timmetje

Reputation: 7694

If you need a total for all vowels @Bohemian has an excellent answer. If you need them seperate it might be easier to do the following:

Just create 1 big string with all the user input.

Then for example when you end up with:

String userInput = 'JohnMaryLisaPeter';


for(int x = 0; x <= userInput.length() - 1; x++) {
    if(userInput.charAt(x) == 97)
        vowelA++;
    else if(userInput.charAt(x) == 101)
        vowelE++;
    else if(userInput.charAt(x) == 105)
        vowelI++;
    else if(userInput.charAt(x) == 111)
        vowelO++;
    else if(userInput.charAt(x) == 117)
        vowelU++;   
} 

System.out.println("There were " + vowelA + " A's in all your names.");
System.out.println("There were " + vowelE + " E's in all your names.");
System.out.println("There were " + vowelI + " I's in all your names.");
System.out.println("There were " + vowelO + " O's in all your names.");
System.out.println("There were " + vowelU + " U's in all your names.");

Upvotes: 0

Bohemian
Bohemian

Reputation: 424993

Never mind all that code. You only need one line:

String[] word = new String[1000]; // given this

int vowels = Arrays.toString(word).replaceAll("(?i)[^aeiou]", "").length();   

This first converts the array to a string (basically a csv), then replaces all non-vowels (fyi (?i) is the case-insensitive flag) with nothing (ie deleting them), then with only vowels left just take the length.

Upvotes: 4

Related Questions