arujbansal
arujbansal

Reputation: 117

How do I print out the list of vowels, numerics etc?

This program takes in a statement and then print the number of vowels, numerics, etc. I wanted to know how I could print the list of numerics and vowels which have been given as input. Can someone tell me how this could be done using an array?

package strings;
import java.util.Scanner;

public class Strings
{
    public static void main(String [] abc)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a statement:");
        String statement = sc.nextLine().toLowerCase();
        System.out.println("------------------------------------------------");
        System.out.println("Total characters: " + statement.length());
        int vowels = 0,num = 0,spaces = 0,spl = 0,others = 0;

        char alpha;

        for (int i = 0;i < statement.length();i++)
        {
            alpha = statement.charAt(i);
            if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u')
            {
                vowels++;
            }
            else if (alpha == ' ')
            {
                spaces++;
            }
            else if (alpha == '0' || alpha == '1' || alpha == '2' || alpha == '3' || alpha == '4'  || alpha == '5' || alpha == '6' || alpha == '7' || alpha == '8' || alpha == '9')
            {
                num++;
            }
            else if (alpha =='!' || alpha =='@' || alpha =='#' || alpha =='$' || alpha =='%' || alpha =='^' || alpha =='&' || alpha =='*' || alpha =='(' || alpha ==')')
            {
                spl++;
            }
            else
            {
                others++;
            }
        }
        System.out.println("Total vowels: " + vowels);
        System.out.println("Total spaces: " + spaces);
        System.out.println("Total numerics: " + num);
        System.out.println("Total special characters: " + spl);
        System.out.println("Other characters: " + others);
        System.out.println("");



        for (int i = 0;i < statement.length();i++)
        {
            System.out.println("The vowels are as follows:");
            System.out.println(alpha);
        }
    }      
}

Upvotes: 1

Views: 241

Answers (2)

Roshana Pitigala
Roshana Pitigala

Reputation: 8786

If in case you don't want to use any external packages.

....

Character v[] = new Character[statement.length()];

....

if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u'){
    v[vowels++] = alpha;
}

....

System.out.println("The vowels are as follows:");
for (Character c : v) {
    if (c != null) {
        System.out.println(c);
    }
}

Upvotes: 0

xingbin
xingbin

Reputation: 28279

You can use java.util.ArrayList, for example, create an ArrayList for vowels:

List<Character> vowelList = new ArrayList<>();
...
for (int i = 0; i < statement.length(); i++) {
    if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u') {
         vowels++;
         vowelList.add(alpha);
     } else {

     ...
}
System.out.println("The vowels are as follows:");
System.out.println(vowelList);

Upvotes: 1

Related Questions