Hashmap Problems

Discussion in 'Plugin Development' started by Snowl, Feb 11, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Snowl

    Code:
                           HashMap<String, String> IP = new HashMap<String, String>();
                            g = (l.split(" "))[1];
     
                            if ( g.startsWith("/here/here") )
                            {
                                String[] parts = g.split("=");
                                if(parts.length == 3)
                                {
                                    if(List.containsKey(parts[1]))
                                    {
                                String part2 = List.getString(parts[1], parts[2]);
    
                                        if(part2.contentEquals(sha512me(parts[2])))
                                        {
                                            //PUT THE DAMN THING INSIDE THE DAMN HASMAP PLEASE! WOWWWWWWWWW
                                            IP.put("test", "test");
                                            json = "<head><meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=/\"></head>";
                                        }
                                        else
                                        {
                                    json = "fail";
                                        }
                                    }
                                    else
                                    {
                                    json = "fail";
                                    }
                                }
                                else
                                {
                                    json = "fail";
                                }
                                print(json);
    
                            }
    A snippet of code from a recent project. If I put "IP.put("test", "test");" anywhere except outside of the if statements, it doesn't put it into the hashmap, it just does nothing. Does anyone know why? What am I doing wrong D:
     
  2. Offline

    Mixcoatl

    The only guess I could hazard, assuming this code, in production, is exactly as it is here, is that one of the expressions on the previous line is throwing an exception that's being caught and ignored outside of this method somewhere.
    There is another possibility with HashMaps: mutable keys can lead to the perception that a key spontaneously appears or disappears after being added. If your production code is using a mutable key, you may want to change that.
     
  3. Offline

    Snowl

    It's not an exception, my code isn't catching anything and if I put a System.out it does execute there.
    Whoops that was my fail. But it still doesnt work :S
    --- merged: Feb 12, 2011 12:38 AM ---
    Oh, I failed >_> it does execute >_> But doing a else if (IP.containsValue("test") == false) will always execute, no matter if its in the hashmap or not :S
     
  4. Offline

    Mixcoatl

    What does the hashmap contain if you print it out or log it? Is it's size consistent with its contents?
     
  5. Offline

    Snowl

    it prints out {test=test} if I request it inside the if statement that puts it in it returns true, but if I try and request it inside another if statement it returns false
     
  6. Offline

    fullwall

    Can you try
    if(IP.get("string") == null)
    // the hashmap doesn't contain "string"
     
  7. Offline

    Mixcoatl

    This may seem like a silly question, but how is the variable scoped?
    Code:
    Map<String, String> map = ...;
    if (x) {
        map.put("x", "y");
    } else {
        if (map.containsKey("x)) {
            // Never true.
        }
    }
    If this is what you're seeing you'll need to move the map outside of the method.
     
Thread Status:
Not open for further replies.

Share This Page