-4

I am taking input from the user. I have that bit done. The input can be a word or even a sentence saved as a string. What I want to do is then count the number of times the letters appear in the input, and also have it sorted alphabetically.

Example input:

learning to program

Example output:

a 2
e 1
g 2
i 1
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Daniel O A
  • 145
  • 7
  • 9
    Note that you've not actually asked a programming question about your code yet. Just wanting to do something is not enough to post to Stackoverflow: what have you tried so far, and how did that not work? And observing the "search and research first" part of [asking a good question](/help/how-to-ask), did you search the web for this first? Because if I search for "java sort characters in a string" I immediately get loads of hits that all explain how to do this. – Mike 'Pomax' Kamermans Feb 11 '20 at 00:12

4 Answers4

1

I wrote some code for you which should do the trick :)

 String name = "doodleice";

    HashMap<Character, Integer> charMap = new HashMap<>();

    char[] charArray = name.toCharArray();

    for(int i = 0; i < charArray.length; i++){
        if(charMap.containsKey(charArray[i])){
            charMap.put(charArray[i], charMap.get(charArray[i]) + 1);
        }
        else{
            charMap.put(charArray[i], 1);
        }
    }

    ArrayList<Character> charList = new ArrayList<>();
    for(Map.Entry<Character, Integer> entry: charMap.entrySet()){
        charList.add(entry.getKey());
    }

    Collections.sort(charList);

    for(int i = 0; i < charList.size(); i++){
        System.out.println(charList.get(i) + " " + charMap.get(charList.get(i)));
    }
manzk
  • 586
  • 1
  • 4
  • 11
1

How to count occurences in a string is explained here: https://stackoverflow.com/a/881111/8935250 I tried to mimic your example output in this code fiddle.

Methods I used: Sort Map

const string = "learning to program"

function count(character) {
 return string.split(character).length
}

map = string.split("").map(c => {
 return {c, count: count(c)}
})

map.sort((a,b) => b.count - a.count)

console.log(map)
console.log(string.split("").sort((a,b) => string.split(b).length - string.split(a).length))

should also do the job but does not show the occurrences.

Rein F
  • 320
  • 3
  • 9
0

One of the Simple solution for sorted String "aabbbcddeeee" then output: a2b3c1d2e4

public static void main(String[] args) {
            
    String input = "aabbbcddeeee"; // output: a2b3c1d2e4
    int count = 0;
    int i = 0;
    int k = 0;
    
    for (i = 0; i < input.length(); i++) {
        
        for (int j = i; j < input.length(); j++) {
            
            if (input.charAt(i) == input.charAt(j)) {
                
                count = count + 1;
                k++;
            } else
                break;
        }
        System.out.print(input.charAt(i));
        System.out.print(count+"\n");
        i = k - 1;
        count = 0;     // reset counter
    }
}
0

How about this;

const test ="the quick brown fox jumps over the lazy dog";
const letterMap = {};
[...test].forEach(x=>{letterMap[x]?letterMap[x]++:(letterMap[x]=1)});
console.log (Object.keys(letterMap).sort().map((key) => [key, letterMap[key]]));

Output;

[[" ",8],["a",1],["b",1],["c",1],["d",1],["e",3],["f",1],["g",1],["h",2],["i",1],["j",1],["k",1],["l",1],["m",1],["n",1],["o",4],["p",1],["q",1],["r",2],["s",1],["t",2],["u",2],["v",1],["w",1],["x",1],["y",1],["z",1]]
Kemal Kaplan
  • 932
  • 8
  • 21