Results 1 to 10 of 10

Thread: HashMap trouble

  1. #1
    Drareeg is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default HashMap trouble

    SOLVED
    Hello, I'm writing a method which is supposed to count how many times every character in a giving string exists.

    So far I've written this code:

    Java Code:
        public void countChars(String input){
            char[] charArray = input.toCharArray();
            HashMap<String,Integer> charMap = new HashMap<String,Integer>();
            for(char karakter : charArray){
                charMap.put(karakter + "" , charMap.get(karakter) == null ? 1: charMap.get(karakter)+1);
            }
            Set set = charMap.keySet();
            Iterator it = set.iterator();
            
            while(it.hasNext()){
                String next = it.next().toString();
                System.out.println(next+": " + charMap.get(next));
            }    
        }
    but for a reason unknown to me, it never prints a value higher then 1.
    If I enter "hello" for example it prints:
    e: 1
    o: 1
    l: 1
    h: 1

    Can anybody help me?
    greetz and thx in advance.
    Last edited by Drareeg; 10-28-2010 at 12:15 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

    Default

    HashMap does not accept duplicates, hence the one 'l'

  3. #3
    Drareeg is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    oh, i see, so my code always try to make a new entry of the same character.
    But then... how to raise the amount associated with the entry by 1 if the entry already exists?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Some of these calls to get from the HashMap are taking different parameters:
    Java Code:
    charMap.put(karakter + "" , charMap.get(karakter) == null ? 1: charMap.get(karakter)+1);
    Since karakter + "" != karakter

    Myself, I'd use a HashMap<Character, Integer> and let autoboxing convert char to Character.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    HashMap does not accept duplicates, hence the one 'l'
    This is not the reason for the problem. He's not adding duplicates to the HashMap.

  6. #6
    Drareeg is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    I think u meant something like the next code.
    But now all it returns is:
    h: null
    l: null
    e: null
    o: null


    Java Code:
        public void countChars(String input){
            char[] charArray = input.toCharArray();
            HashMap<Character,Integer> charMap = new HashMap<Character,Integer>();
            for(char karakter : charArray){
                charMap.put(karakter, charMap.get(karakter) == null ? 1: charMap.get(karakter)+1);
            }
            Set set = charMap.keySet();
            Iterator it = set.iterator();
            
            while(it.hasNext()){
                String next = it.next().toString();
                System.out.println(next+": " + charMap.get(next));
            }    
        }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Quote Originally Posted by Drareeg View Post
    I think u meant something like the next code.
    But now all it returns is:
    h: null
    l: null
    e: null
    o: null
    Yep. The Map holds Character keys and you're trying to get the value using a String key.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    10

    Default

    Quote Originally Posted by Fubarable View Post
    This is not the reason for the problem. He's not adding duplicates to the HashMap.
    Can you explain a bit more how they were not duplicates for me Fubarable? how can a Character 'l' that has been added once before be deemed a new Character?

    edit: do'h they are not deemed new, okay I got it now
    Last edited by al_Marshy_1981; 10-28-2010 at 12:16 AM.

  9. #9
    Drareeg is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks a lot fubarable! and thanks for not just giving the solution :) I learned some extra stuff!
    for the people curious for a possible solution
    here's mine:
    Java Code:
        public void countChars(String input){
            char[] charArray = input.toCharArray();
            HashMap<Character,Integer> charMap = new HashMap<Character,Integer>();
            for(char karakter : charArray){
                charMap.put(karakter, charMap.get(karakter) == null ? 1: charMap.get(karakter)+1);
            }
            Set set = charMap.keySet();
            Iterator it = set.iterator();
            
            while(it.hasNext()){
                String next = it.next().toString();
                char volgende = next.charAt(0);
                System.out.println(volgende+": " + charMap.get(volgende));
            } 
        }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

Similar Threads

  1. HashMap Help
    By digitol97 in forum New To Java
    Replies: 4
    Last Post: 09-13-2010, 03:38 AM
  2. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  3. Hashmap trouble
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 11-10-2009, 10:38 PM
  4. HashMap Help
    By ScaryJello in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 10:18 AM
  5. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •