I’ve created a hash table and I’m trying to use enumeration in order to print out the keys as well as the values. When I try to compile the code, I keep getting a compile time error saying i need to put a ‘[‘ in new double’s i’ve put into the hashtable.
Before compile:
toys.put(“Race Car”, new double (29.99));
Compile time error says I need to place it like this:
toys.put(“Race Car”, new double [29.99]);
what am i doing wrong?
public static void main(String[] args) {
Hashtable toys = new Hashtable();
Enumeration toyName;
String getToyName;
double priceOfToy;
toys.put("Race Car", new double(29.99));
toys.put("Toy Bear", new double(15.99));
toys.put("Action Figure", new double(9.99));
//Show prices of each toy
toyName = toys.keys();
//Uses hasMoreElements method from enumeration to check what is in the hashtable
while (toyName.hasMoreElements()) {
//uses nextElement method from enumeration interface to
getToyName = (String) toyName.nextElement();
System.out.println(getToyName + "is now priced at " + toys.get(getToyName));
}
}
Map only accepts Objects, not primitives, double is a primitive and Double is an Object.
and also consider using generic types for your collections: