Hello All,

How can I get the information that corresponds with the containsKey to be put into the hashtable.

I am checking to see if a hashtable contains a key. If it does how can I put that key along with its value into a second hashtable.


Snippet of code:

for(int i = 0; i < 7; i ++)
     {
         if(hash.containsKey("elephant"))
         {
            hash2.put(hash.t.animals[i].getName(), hash.t.animals.item[i]getDescrip());
         }
     }

For example if "hash" containsKey "elephant". Search the array for elephant and "put" "elephant" and its description into hash2. The "elephant" is in .....getName() and description of the elephant is in .....getDescrip().

elephant would be the key and its description would be the value.

Any suggestions would be appreciated.

Thanks,

Not sure what you are doing with the "hash.t..." stuff there and the whole operation seems odd, but this will put "elephant" and it's value from "hash" value into hash2

hash2.put("elephant", ((WhateverClassForAnimal)hash.get("elephant")).getDescrip());

If you typed the HashMaps with generics then you can drop the cast to WhateverClassForAnimal.

Why did you create a new thread and ask the same question? If you didn't understand my answer at the previous thread then ask for more details or ask a better question.

Thanks for your help Ezzaral.

I wrote:

Why did you create a new thread and ask the same question? If you didn't understand my answer at the previous thread then ask for more details or ask a better question.

I would like to apologize to both of you and whoever read this thread. I am sorry for the way I replied and I made a very big and silly mistake. Yesterday I was preparing a reply for this thread but before I submitted my internet connection went down and I could not connect again. So naturally I didn't submit any answer to the question. But I forgot about it, and today when I saw the thread again I thought that it was a new thread because I was so sure that I submited yesterday, but I didn't.
So I would like to apologize again for my manners and especially to KimJack.

commented: hehe oops! These things happen. +7

No prob JavaAddict,

I started a new thread because the old one started to get a bit confusing and unorganized. I was starting to confuse myself with all of the changes that I was making (thinking that I had figured it out, when actually it just "seemed" that I had).

Thanks for your comments and help.

I have tried the following:

if(test.containsKey("mouse"))
      {
          test2.put("mouse", (test.getDescrip()));
                          
      }

This this code only "mouse" gets put into the hashtable as the key and value is simply null.

I tried the suggestion from Ezzaral, however, that set up will not allow me access to the getDescrip().

Are there any other suggestions or additional information that I can provide?

Any assistance will be greatly appreciated.

Thank you,

"test" is your HashMap. It is not an object of the class that has the getDesript() method. That is why my example showed the cast of test.get(key) to (WhateverClassThoseAre) before making the call to getDescrip(). The value stored by HashMap is Object. You have to cast that to the appropriate type before you can call methods provided by that type (If you are using generics to type the HashMap that cast is essentially made for you).

The "set up" I showed

hash2.put("elephant", ((WhateverClassForAnimal)hash.get("elephant")).getDescrip());

will allow you to call that getDescrip() method if you cast it appropriately - which I cannot show explicitly because I have no idea what class that value is.

Thanks Ezzaral,
It worked exactly as you described however I am now receiving the following error that I am trying to figure out:

Exception in thread "main" java.lang.ClassCastException: java.lang.String

This how I set up the previous code:

if(hash.containsKey(info))
    {
      hash2.put(info,((Item)hash.get(info)).getDescrip());
    }

Thanks to all for your assistance.

At what line do you get it, and can we see some more code (the class Item for example)?
But in general this exception happens when you try to cast something that cannot be casted.
Probably the hash.get(info) does not return Item but String, but I cannot be certain about this if you don't show a little more code and where do you get the error. It could be something else.


PS: I have fixed my internet at home. The modem that the manual was describing was not the one that they have sent, so the line connections where wrong.

This is the line the received the exception:

hash2.put(info,((Item)hash.get(info)).getDescrip());

Well, "info" is infact a substring from a string entered by the user and getDescrip() returns a string from the Item class.

The Item class is alot of code but simply a class full of methods that sets up everything. get this, get that, set this, set that, basic stuff. Here is the getDescrip() method from the Item class:

public String getDescrip()
      {
         return description;
      }

But it also extends a Dictionary class that contains the hashtable that I HAD to create myself. Here is my "homemade" put and get method from the Dictionary class that is extended from the Item class.

Item extends Dictionary
.....

public void put(Object key, Object value)
       {
          int bucket = hash(key); 
          ListNode list = hashTable[bucket];    
          while (list != null)
          {
                
             if (list.key.equals(key))
                break;
             list = list.next;
          }
          if (list != null)
          {
             list.value = value;
          }
          else
          {
             
             if (count >= .50 *hashTable.length)
             {
                doubleHash();
             }
             ListNode newNode = new ListNode();
             newNode.key = key;
             newNode.value = value;
             newNode.next = hashTable[bucket];
             hashTable[bucket] = newNode;
             count++;          }
       }
    
   
   
   
   
       public Object get(Object key)
      {
         int bucket = hash(key); 
         ListNode list = hashTable[bucket]; 
         while (list != null) {
            if (list.key.equals(key))
               return list.value;
            list = list.next; 
         	
         }
         return null;  
      }

That's it. Thanks for all your help.

Glad to hear that you got everything up and running.

Well, since the stack trace tells you the exact problem and the line on which it occurs, this should be easy to correct with some very basic debugging. There is very little casting going on there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.