Results 1 to 5 of 5
  1. #1
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default summing value within Hashtable

    Hello,

    I'm having problem trying to figure out how to use Hashtable data structure. I'm reading names and number of occurrences for that name from text files. I'm using name as key and number of occurrences as value. If the name exist in Hashtable I want to sum the two values and store it back in Hashtable. How do I do that? Here is where I'm at (Bear in mind I'm very new to programming):

    Java Code:
    public class HashTableImplementation {
    	
    	double sum = 0;
    	Hashtable hashList = new Hashtable();
    
    	public HashTableImplementation()
    	{
    		hashList = new Hashtable();
    
    	}
    	
    	public void addToHasTable(String name, double weight)
    	{
    		if (!hashList.containsKey(name))
    			hashList.put(name, weight);
    		else 
    		{
    			//sum weights for that name
                    }
    	}
    Any help would be appreciated.
    Last edited by dc0m; 09-22-2010 at 11:44 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    I want to sum the two values and store it back in Hashtable
    If the key does not exist, create a new object with the data and put it in the hashtable.
    If the key is there, get the value, add the new value to the existing one and put that object back.
    You are using the autoboxing feature of the compiler to put your values in the hashtable. The put method takes two objects. It does NOT take a primitive like a double. The compiler is wrapping your double into a Double.

  3. #3
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for fast response.

    I managed to do that, by doing this.

    Java Code:
    sum = weight + Double.parseDouble(hashList.get(name).toString());
    hashList.put(name, sum);
    But this just adds another Hashtable entry it does not add values to the same entry =/

    Let's say I have Mary, 33, and then I get another Mary with 40. I want to add to "Mary" (which has value 33) value 40. The result would be one Mary,73. But istead I get two Marys, one with 33 and one with 73. Where am I making mistake?

    Thanks again.
    Last edited by dc0m; 09-23-2010 at 12:03 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    I don't know how you can get two keys = Mary.
    Can you post the code.
    Java Code:
    sum = weight + Double.parseDouble(hashList.get(name).toString());
    Putting all the code in one statement is a poor way to do it.

  5. #5
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default [SOLVED]summing value with Hashtable

    Never mind, that actually worked, there was actually totally different reason why it printed out two Marys.

    Thanks a lot friend.

    "Putting all the code in one statement is a poor way to do it. "

    What is the right way to do it?
    Last edited by dc0m; 09-23-2010 at 12:31 AM.

Similar Threads

  1. Help with summing series
    By xplsivo in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 07:37 PM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 08:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •