You are initialising your counters with the character position:
myMap.put(ch, i);
where you want:
myMap.put(ch, 1);
You also want to check the map for an already existing character before you initialise the counter, incrementing the counter of it does (using get(ch)
not get(i)
), giving:
char ch = s.charAt(i);
//calculating the occurence of a character in a string.
if (myMap.containsKey(ch)){
myMap.put(ch, myMap.get(ch) + 1);
} else {
myMap.put(ch, 1);
}//end of if statement