If you construct in hashmap.put call
I have a variable of type Hashmap <String,Integer
>.
In this, the Integer value may be required for some manipulation depending on the value of the flag variable. I did it like this ...
Hashmapvariable.put( somestring,
if (flag_variable) {
//manipulation code goes here
new Integer(manipulated value);
} else {
new Integer(non-manipulated value);
}
);
But I am getting the error:
Syntax error on tokens (s), inappropriate construction.
in the call to Hashmapvariable.put.
I also get another error
Syntax error on token ")", remove this token.
in the final "); line. But I can't remove the") "- its the closing parentheses to call the put method.
I do not understand. What mistake am I making?
source to share
You cannot put a statement in a method call.
However, one option would be to create a method that returns Integer
, for example:
private Integer getIntegerDependingOnFlag(boolean flag)
{
if (flag)
return new Integer(MANIPULATED_VALUE);
else
return new Integer(NON-MANIPULATED_VALUE);
}
Then you can make a call like this:
hashmap.put(someString, getIntegerDependingOnFlag(flag));
source to share