Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Problems getting value from map

    I want to retrieve the value from a hashmap corresponding to some key, but I have some problems getting it working.

    Based on what I've seen in several examples, I'd say that something like this would do it:

    Java Code:
    int nGramCount = 1;
    					int newnGramCount = 0;
    					// System.out.println("This is the nGram: " + nGram);
    					if (!nGramMap.containsKey(nGram)){
    						nGramMap.put(nGram, nGramCount);
    					}
    					else {
    						newnGramCount = nGramMap.getValue(nGram);
    						nGramMap.put(nGram, nGramCount);
    					}
    What I want to express is the following:
    I'm looping through some strings, put these strings in a map and assign them a count. So if I bump into some string that is not already in the map, stuff it there and assign it the nGramCount. If the string already is in the map, the count of that string/key should be raised. So I look up the value of this string/key, add 1 to it, and then want to put it back in there.

    The error message I get:
    The method getValue(String) is undefined for the type HashMap<String,Integer>

    Help is much appreciated!

  2. #2
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    Try to use get(String str). There is no method getValue(String str) in HashMap.

    V get(Object key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    I don't see the +1 and I don't see any types declarations. You're also not doing anything with newnGramCount. Count we see a more fleshed out example?

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. RMI Problems
    By michaeln31 in forum New To Java
    Replies: 4
    Last Post: 10-23-2010, 12:52 PM
  3. Problems here
    By Keno777 in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 11:35 AM
  4. Few Problems Help!
    By elad_bj in forum New To Java
    Replies: 4
    Last Post: 04-07-2009, 03:45 AM
  5. gui problems
    By bluebirdjc in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 05:38 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
  •