![]() |
![]() |
#1 (permalink) |
Crazy
|
[JAVA] Need to add 'double' to hashtable
Here is what i'm working with:
Code:
public class Menu1DB { public static Hashtable loadMenu() { int i = 0; String line; Hashtable hshProdDesc = new Hashtable(); Hashtable hshProdPrice = new Hashtable(); StringTokenizer strings; try { BufferedReader file = new BufferedReader(new FileReader("menu.txt")); // priming read line = file.readLine(); //while ((line = file.readLine()) != null) while (line != null) { strings = new StringTokenizer(line, ","); String strProdID = strings.nextToken(); String strProdDesc = strings.nextToken(); String strProdPrice = strings.nextToken(); double dblProdPrice = Double.valueOf(strProdPrice).doubleValue(); hshProdDesc.put(strProdID,strProdDesc); //-->> hshProdPrice.put(strProdID,dblProdPrice); Thanks! -BD |
![]() |
![]() |
#2 (permalink) |
Tilted
|
The Double class is derived from object, you need to use that since Java 1.4- doesn't support putting primitives inside hashtables. Something like
Code:
String strProdPrice = strings.nextToken(); Double dblProdPrice = Double.valueOf(strProdPrice); hshProdDesc.put(strProdID,dblProdDesc); |
![]() |
![]() |
#3 (permalink) |
Crazy
Location: The state of denial
|
a-j hit the nail on the head there. On a side note, it looks like you are adding both the product Id and the price under the same Hash table key. This won't work. Each item in a hash table needs to have a different key. Maybe you should make a "Product" class that holds all the information about a product (Description, Id and Price from what I can see) and store that in your Hash table.
__________________
Smoke me a kipper, I'll be back for breakfast. |
![]() |
Tags |
add, double, hashtable, java |
|
|