I am trying to count occurrence of each character in a string. So if I input aaabbbccc I am expecting o/p as {a=3, b=3, c=3} but instead I keep on getting it as {a=1, b=1, c=1} as my hashtable contains method fails and returns false. What is wrong in the code?
I also know that there is a HashMap collection quite similar to hashtable. but as I am learing java I want to try the same code with all datastructures just to get an idea of methods. The code with map is not much different than what I am doing here still this code fails. and I am not able to figure out where is the bug in the code.
I have following code:
Hashtable<Character, Integer> stringHash = new Hashtable<Character, Integer>();
This stringHash is a class level variable.
for(int i=0; i<s.length(); i++){
        if(stringHash ==null || stringHash.size()==0){
            stringHash.put(s.charAt(i), 1);
        }
        else{
            if(! stringHash.contains(s.charAt(i)) ){
                System.out.println(s.charAt(i));
                stringHash.put(s.charAt(i), 1);
            }
            else{
                int count = stringHash.get(s.charAt(i));
                stringHash.put(s.charAt(i), count++);
            }   
        }
        System.out.println(stringHash + " " + s.charAt(i) + "  "+ stringHash.contains(s.charAt(i)));
}
 
     
     
     
     
     
     
    