Home > OS >  Is there a way to attach a character label with a sorted array output?
Is there a way to attach a character label with a sorted array output?

Time:10-02

I have to create a vowel counter and sorter, where someone can input a word or phrase and the program picks out, counts, and sorts the vowels. I have the code to where it counts and sorts the variables and shows their counts to the user, but it doesn't say which vowel has which count and I have exhausted all of my resources. I am very new to coding and know very little, so if there's anything anyone can do to help, I would appreciate it endlessly.

int[] vowelcounter = {a, e, i, o, u}; //This is the count of the vowels after reading the input.
   boolean hasswapped = true;
    while(hasswapped)
    {
        hasswapped = false;
        for(int j = 0; j<vowelcounter.length; j  )  
            {
            for(int k = j 1; k<vowelcounter.length; k  )
                {
                    if(vowelcounter[j] > vowelcounter[k])
                    {
                        int temp = vowelcounter[j];
                        vowelcounter[j] = vowelcounter[j 1];
                        vowelcounter[j 1] = temp;
                        hasswapped = true;
                    }
                }
            }
    }
        
        for(int j=0; j<vowelcounter.length; j  )
            {   
                    System.out.println(vowelcounter[j]);
            }

CodePudding user response:

You can use something that is called HashMap

HashMap<String, Integer> vowelCounts = new HashMap<>();

To add data to it just do:

vowelCounts.put("a", 1); // The vowel "a" is once in the sentence
vowelCounts.put("e", 2); // The vowel "e" is 2 times in the sentence

To print to the console:

for(String vowel : vowelCounts.keySet() ) {
     System.out.println(vowel   ": "   vowelCounts.get(vowel));
}

For more info: click me!

CodePudding user response:

Have a char[] vowels = { 'a', 'e', 'i', 'o', 'u' }. Every time you swap the counters, make an identical swap in the vowels array.

int temp = vowelcounter[j];
vowelcounter[j] = vowelcounter[j 1];
vowelcounter[j 1] = temp;
char temp2 = vowel[j];
vowel[j] = vowel[j 1];
vowel[j 1] = temp2;
hasswapped = true;

At the end, print out vowel[j] next to vowelcounter[j];

CodePudding user response:

Instead of int value to represent a counter, a class may be introduced to store and print both the vowel character and its count:

class VowelCount {
    private final char vowel;
    private int count = 0;

    public VowelCount(char v) {
        this.vowel = v;
    }

    public void add() { this.count  ; }

    public int getCount()  { return this.count; }

    public char getVowel() { return this.vowel; }

    @Override
    public String toString() { return "Vowel '"   vowel   "' count = "   count;}
}

Then instead of int[] count an array of VowelCount is created and sorted:

VowelCount[] vowelcounter = {
    new VowelCount('a'), new VowelCount('e'), new VowelCount('i'), 
    new VowelCount('o'), new VowelCount('u')
};

Sorting may be implemented using standard method Arrays::sort with a custom comparator instead of home-made bubble sorting

Arrays.sort(vowelcounter, Comparator.comparingInt(VowelCount::getCount));

Then printing of the stats is as follows (using for-each loop along with the overriden toString):

for (VowelCount v: vowelcounter) {
    System.out.println(v); // print sorted by count
}

More advanced ways of calculating the frequencies is to use a map of vowels to their frequencies and sort the map by counter value.

  • Related