0

I have a class written that takes a string, counts the occurrences of each letter within the string and then prints the occurrences of each. I want this to be displayed alphabetically, but not sure how to do this.

import java.util.ArrayList; // import the ArrayList class

class CryptCmd {

    public static void CryptCmd(String str) {

        ArrayList<String> occurs = new ArrayList<>();

        final int MAX_CHAR = 256;

        // Create an array of size 256 i.e. ASCII_SIZE
        int[] count = new int[MAX_CHAR];

        int len = str.length();

        // Initialize count array index
        for (int i = 0; i < len; i++)
            count[str.charAt(i)]++;

        // Create an array of given String size
        char[] ch = new char[str.length()];
        for (int i = 0; i < len; i++) {
            ch[i] = str.charAt(i);
            int find = 0;
            for (int j = 0; j <= i; j++) {

                // If any matches found
                if (str.charAt(i) == ch[j])
                    find++;
            }

            if (find == 1)
                occurs.add("Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n");
        }

        System.out.println(String.join("",occurs));

        int total = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != ' ')
                total++;
        }
        System.out.println("Total chars is " + total);
    }
}

So far the print displays in the order the letter was found, i.e

"Hello" = 
Number of Occurrence of H is: 1
Number of Occurrence of e is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1

Total chars is 5

The desired output is this, ordered alphabetically i.e

"Hello" =
Number of Occurrence of e is: 1
Number of Occurrence of H is: 1
Number of Occurrence of l is: 2
Number of Occurrence of o is: 1

Total chars is 5
Scott Adamson
  • 385
  • 1
  • 3
  • 10

3 Answers3

1

It returns because H is capital in your case which is before to small letters in ASCII ordering.

A - 65 - starts at

a - 97 - starts at

That's the reason.

If you maintain one case, then it will be proper. Otherwise you have to write your own comparator order something similar to this.

Gibbs
  • 21,904
  • 13
  • 74
  • 138
1

This is the code


import java.util.*; // import the ArrayList class


class CryptCmd1 {
    public static void main(String[] args) {
        CryptCmd("Hello");
    }
    public static void CryptCmd(String str) {

        ArrayList<String> occurs = new ArrayList<>();

        final int MAX_CHAR = 256;

        // Create an array of size 256 i.e. ASCII_SIZE
        int[] count = new int[MAX_CHAR];

        int len = str.length();

        // Initialize count array index
        for (int i = 0; i < len; i++)
            count[str.charAt(i)]++;

        // Create an array of given String size
        char[] ch = new char[str.length()];
        for (int i = 0; i < len; i++) {
            ch[i] = str.charAt(i);
            int find = 0;
            for (int j = 0; j <= i; j++) {

                // If any matches found
                if (str.charAt(i) == ch[j])
                    find++;
            }
            String outString = "Number of Occurrence of " + str.charAt(i) + " is: " + count[str.charAt(i)] + "\n";

            if (find == 1)
                occurs.add(outString.toLowerCase());
        }
        Collections.sort(occurs);
        System.out.println(String.join("",occurs));

        int total = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != ' ')
                total++;
        }
        System.out.println("Total chars is " + total);
    }
}

I have used the collections class to sort the array and fulfill your requirement

Suparno Saha
  • 115
  • 1
  • 10
0

Before printing just add below sorting logic

Collections.sort(occurs, (a,b) -> a.compareToIgnoreCase(b));
Eklavya
  • 17,618
  • 4
  • 28
  • 57
Alok Singh
  • 488
  • 2
  • 6