I am getting an compilation error "not a statement" for the below code. Not sure what's wrong. inMax is a hasmap. tcharge is a string and it's a key. Is this one a valid statement?
Double tMaxCharge= (Double)inMax.get(tCharge);
I am getting an compilation error "not a statement" for the below code. Not sure what's wrong. inMax is a hasmap. tcharge is a string and it's a key. Is this one a valid statement?
Double tMaxCharge= (Double)inMax.get(tCharge);
Java's compiler isn't always as helpful as it thinks. Look at other lines nearby where it points you. The problem may be on the line before.
I agree with objects' answer, but to avoid casting, can you just use generics in this case, eg:
HashMap<String,Double> myMap = new HashMap<String,Double>();
myMap.put("foo", 3.14); //or new Double(3.14)
myMap.get("foo") //evaluates to type Double (and can be autoboxed to a double)
Apparently you code was something like:
if (someCondition)
Double tMaxCharge= (Double)inMax.get(tCharge);
else
doSomething();
As @objects says, that is not valid Java syntax. A LocalVariableDeclarationStatement
is a BlockStatement
but not a Statement
and an if
statement requires a Statement
following the )
.
But if you think about it, a LocalVariableDeclarationStatement
that it not inside a Block
is pointless. The scope of the declaration would end immediately, so the declared identifier could not be used. (And it has to end immediately or you would get all sorts of linguistic anomalies.)