views:

72

answers:

2

I try to do it in the following way:

public String getValue(String service, String parameter) {
    String inputKey = service + ":" + parameter;
    Set keys = name2value.keySet();
    Iterator itr = keys.iterator();
    while (itr.hasNext()) {     
        if (inputKey.equal(itr.next())) {
            return name2value.get(inputKey);
        }
        return "";
    }
}

And I get an error message: cannot find symbol method.equal(java.lang.Object).

I think it is because itr.next() is not considered as a string. How can I solve this problem? I tried to replace Set keys by Set<String> keys. It did not help.

+7  A: 

The method you want is called equals not equal.

However there are a few other flaws in your code.

Firstly you should not be looping through all keys in a Map to find a specific key, just use get and/or containsKey.

The second return is also wrong. It will return "" if the first key does not match. If you want to return "" when none of the keys match, the return should go at the end of the method e.g.:

public String getValue(String service, String parameter) {
    String inputKey = service + ":" + parameter;
    String value = name2value.get(inputKey);
    if (value == null) {
        return "";
    } else {
        return value;
    }
}
finnw
A: 

Firstoff,The method is equals() Secondly, cast itr.next() to string instead of Set.

Vaishak Suresh