Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [JAVA] Need to add 'double' to hashtable (https://thetfp.com/tfp/tilted-technology/83536-java-need-add-double-hashtable.html)

BoltedDown 02-15-2005 10:21 PM

[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);

Apparently '.put' only allows (Object, Object) so i'm stuck. I need to do (Object, double).

Thanks!

-BD

a-j 02-15-2005 11:35 PM

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);


madcow 02-16-2005 05:20 PM

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.


All times are GMT -8. The time now is 01:49 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60