Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Hashtable Java

Options
  • 19-04-2006 9:20pm
    #1
    Registered Users Posts: 7,677 ✭✭✭


    I am an online store where I am using a hashtable for the shopping basket.

    My web page is JSP and I have Java Class file which is seperate from the web page.

    Everytime I add something to my hashtable my hashtable is empty.

    Can someone help me to keep the information in my hashtable without clearing it down.

    I probably shouldn't have

    Hashtable hashtable = new Hashtable();


    JSP Page
    Basket basket = new Basket();

    basket.addEnteries(intID);


    java class

    public void addEnteries (int intItemID)
    {
    int intID;
    intID = intItemID;
    int hashSize = 0;

    Hashtable hashtable = new Hashtable();

    hashSize = hashtable.size();
    hashSize = hashSize + 1;
    String key = String.valueOf(hashSize) ;
    String value = String.valueOf(intID);

    if (!hashtable.containsKey(key))
    {
    hashtable.put(key, value);
    }

    }


    If more information is required I will supply

    Thanks for your help


Comments

  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    The simple answer is that you are right about this line:
    Hashtable hashtable = new Hashtable();
    

    The longer answer is that that definitely isn't your only problem.

    Just a quick question, each ID is unique isn't it? Wouldn't it be far easier then to use that as your key? The value could then be the number of that item required. One of the main features of a hash table is that you actually have no need to know its size at all, so that can go.

    Your addEntries method would be something like this:
    public void addEntry(int itemID,  int numItems)
    {  
        if(!hashTable.containsKey(itemID))
        {
            hashtable.put(itemID, numItems);
        }
    }
    
    [edit] You may also want to add an else statement, to inform your user that a duplicate item exists.


  • Registered Users Posts: 7,677 ✭✭✭Trampas


    Thanks for the response and help

    I was going to do that put a person will be allowed to add the same item twice so that why I was heading down the size line.

    I still need to declare the hashTable in the code don't I??


  • Closed Accounts Posts: 80 ✭✭Torak


    You need to do two things:

    1. Create the basket as a session scope variable instead of every time on the page..
    <jsp:useBean id="basket" scope="session" class="Basket" />
    

    If you don't you will have the same problem. You will need to research this further to get it to work. The sun site is a good place to start.

    2. Change the Hashtable to be a field in the class.


  • Closed Accounts Posts: 521 ✭✭✭EOA_Mushy


    Blowfish wrote:
    Your addEntries method would be something like this:
    public void addEntry(int itemID,  int numItems)
    {  
        if(!hashTable.containsKey(itemID))
        {
            hashtable.put(itemID, numItems);
        }
        else{
            int x = hashtable.get(itemID);
            x += new amount of item
            hashtable.put(itemID,x);
       }
    }
    

    g


Advertisement