Saturday, July 2, 2011

Hash map exception in java ClassCastException

Hash map exception in java ClassCastException


On execution of the code, the code show you an exception indicating an string cannot be converted into double.


PROGRAM


import java.util.*;
import java.util.Iterator;
public class Hashmapexception {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put("ABS", new Double(3434.34));
hashMap.put("ABD", new Double(123.22));
hashMap.put("cccc", "jdshgjhs");
Set set = hashMap.entrySet();
Iterator i = set.iterator();
while (i.hasNext()) {
Map.Entry me = (Map.Entry) i.next();
System.out.println(me.getKey() + " : " + me.getValue());
}
double balance = ((Double) hashMap.get("cccc")).doubleValue();
hashMap.put("cccc", new Double(balance + 1000));
System.out.println("balance : " + hashMap.get("cccc"));

}
}


OUTPUT


Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at Hashmapexception.main(Hashmapexception.java:20)
Java Result: 1

No comments:

Post a Comment