Solved

Can't retrieve the data from hash table

Posted on 2004-04-15
46
664 Views
Last Modified: 2010-03-31
I want to use a hash map to storing my data,after I input data. I can't retrieve the data..
It will retuen null...
How to correct it...?
Here is part of my code.
Thank you!


AssestTrackerUI:
public class AssestTrackerUI extends JFrame {
  int id=1;
  public static void main(String[] args) {
    AssestTrackerUI dicMain = new AssestTrackerUI();
    dicMain.setSize(800,600);
    dicMain.setVisible(true);
    dicMain.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
            System.exit(1);}});
  }

  void butSearch_actionPerformed(ActionEvent e) {

      ElectronicApplicance type=new ElectronicApplicance();
      taSearch.setText(type.idsearch(Integer.parseInt(txtSearch.getText())));
}
   }



  void butEnter_actionPerformed(ActionEvent e) {
        String name="";
    if(txtEnter.getText().equals("")||taEnter.getText().equals(""))
       taEnter.setText("Enter both owner name and asset name");
    else{

if(jComboBox1.getSelectedIndex()==0){
              ElectronicApplicance type=new ElectronicApplicance();
              type.set(id,txtEnter.getText(),taEnter.getText());
             
              type.asset(jTextField7.getText());
             

            }

       id=id+1;
       }
  }
}


DicMain:
abstract class DicMain{
      String name="";
      String a,b,c,d;
      String Owner,AssetName,AppType;
      int Id;
      String Add;
 

  HashTable has=new HashTable();

  public DicMain() {
   
  }

  public void set(int id,String owner,String assetname) {
        Owner=owner;
      Id=id;      
    AssetName=assetname;
  }

public abstract void asset(String add);
public abstract String type();

public void insert(){

       has.addEntry(new Integer(Id),name);
            }

public String idsearch(int id){

      
      return has.getEntry(new Integer(id));
      }      
}



Hash Table:
class HashTable {

  public void addEntry(Integer a, String b) {

    phonebook.put(a,b);
   }
    public String getEntry(Integer c) {


          return (String)phonebook.get(c);
  }
}


ElectronicApplicance:
class ElectronicApplicance extends DicMain{

    public ElectronicApplicance( ) {
    }
    public void asset(String add){
            Add=add;
            name="ElectronicApplicance: "+AssetName+"\n"+"appliance type: "+Add;
            insert();
    }
}
0
Comment
Question by:Terry_hk
  • 20
  • 20
  • 4
  • +1
46 Comments
 
LVL 3

Expert Comment

by:int_20h
ID: 10835921
Is Hashtable your implementation?  Are you using or inheriting from the java.util.Hashtable?
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10835928
class HashTable {

  public void addEntry(Integer a, String b) {

    phonebook.put(a,b);
   }
    public String getEntry(Integer c) {


         return (String)phonebook.get(c);
  }
}
is phonebook a java.util.Hashtable member variable ofr HashTable?
0
 

Author Comment

by:Terry_hk
ID: 10835983
yes @@"
sorry..forget to copy it..

class HashTable {
     private Map phonebook = new HashMap();

  public void addEntry(Integer a, String b) {

    phonebook.put(a,b);
    System.out.println(a+"\n"+b);
  }
    public String getEntry(Integer c) {


          return (String)phonebook.get(c);
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836227
It's a little difficult to see from the code you've posted, but at a guess (guessing that ultimately the actual Hashtable is contained by ElectronicApplicance), since you create a new instance of ElectronicApplicance on each invocation of your event handler, you will always start with an emtpy Hashtable. You  should create just one instance of ElectronicApplicance
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10836257
The code looks good... What I would recommend is to write a driver to test your HashTable individually.  Also, if you use an IDE you put a breakpoint in your getEntry method...
Oh, after looking at the code again... It makes me think that the hashmap might be looking at the pointer reference...  Can you try converting the int into String as the key in the hashtable (Which I know it works)?
0
 

Author Comment

by:Terry_hk
ID: 10836377
User can select different type of asset(ElectronicApplicance is one of type), so I need calling different abstract class when user input a new item.

How to create just one instance of ElectronicApplicance?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836397
Create it in one place, say in the constructor of your application. Maybe you need to create one of each type and switch between them as appropriate
0
 

Author Comment

by:Terry_hk
ID: 10836503
That mean I should control the hash map in AssestTrackerUI?
If I do that, DicMain seems nothing to do @_@


Part of my exercise:
Each asset will have a unique serial number (called tracking ID), which starts from 1, an owner name and its value (of double type). Depending on the type of the asset, there may be additional information

Asset: an abstract super-class.this super-class should contain all the common features shared by all types of assets. An abstract method called toString should be specified in this class. Appropriate constructor(s), mutator(s) and assessor(s) should be provided.

ElectronicApplicance: concrete classes containing data members and methods specific certain types of assets. Similarly, constructor(s) Appropriate constructor(s), mutator(s) and assessor(s) should be provided.

AssetTracker: modeling a data store.This class should contain a hash map objects In this class, methods for retrieving information from the data store and updating the data store should be provided.

AssestTrackerUI: This class employs some common GUI, like JMenu, JButton, Jlabel, JTextField, and JTextArea to provide a graphic interface for users to assess the utilities for manipulating the data store (i.e. an object of AssetTracker).


AssestTrackerUI should only provide gui and event handle....?
I should hash map in DicMain..?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836716
Ignore my earlier comments- i misinterpreted what was happening in the program. But you do need to clarify the entities. For instance, you have a class called DicMain, yet the instance variable of AssetTrackerUI is called 'dicMain'. This is confused.

>>AssetTracker: modeling a data store.This class should contain a hash map objects In this class

This means there should be a class called 'AssetTracker' that contains a HashMap. There's no need to invent your own HashTable class.


>>AssestTrackerUI should only provide gui and event handle....?

Yes


>>I should hash map in DicMain..?

What is DicMain and how does it come into things?
0
 

Author Comment

by:Terry_hk
ID: 10836778
This means there should be a class called 'AssetTracker' that contains a HashMap. There's no need to invent your own HashTable class.

Change HashTable name to be AssetTracker..?(forget change it @_@)
then...?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836813
>>Change HashTable name to be AssetTracker..?(

I don't see why not. Of course it will have to have the correct relationship with the other entities too
0
 

Author Comment

by:Terry_hk
ID: 10836826
ok
but the program still can't work correctly...how to correct it...?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836885
OK - let's take another look - where is the value null that you're expecting not to be? If there is an exception, please post the stack trace
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836915
btw ElectronicAppliance should be a subclass of Asset
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10837057
>>Ignore my earlier comments- i misinterpreted what was happening in the program

LOL - sorry. Ignore that too - i was right in the first place
0
 

Author Comment

by:Terry_hk
ID: 10837074
I input a haspmap key, and I want it return a hashmap value only. But it return a value "null" , it is not an exception.
I modify the code and retrieve the data when I input a data.
Integer a is 1

  public void addEntry(Integer a, String b) {
    phonebook.put(a,b);
   System.out.println((String)phonebook.get("1"));
  }
It return null

  public void addEntry(Integer a, String b) {
    phonebook.put(a,b);
   System.out.println((String)phonebook.get(1));
  }
Error


  public void addEntry(Integer a, String b) {

    phonebook.put(a,b);
    System.out.println((String)phonebook.get(a));
  }
It can return correct answer

Why @_@?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10837125
See earlier comment - you are creating a new ElectronicAppliance and therefore a new (empty) hash map each time.

My advice is not to worry too much about the fact it's not working at the moment. You need to rethink the design. For one thing, ElectronicAppliance should NOT contain a hash map. An ElectronicAppliance should be maintained in the hash map of an AssetTracker.
You should get each entity working independently first and then bring them together
0
 

Author Comment

by:Terry_hk
ID: 10837242
This program include 4 types of assets, so I need creat a new class(ElectronicAppliance) .

Does the exercise means each asset has one map?
Or 4 assets share 1 map?




0
 

Author Comment

by:Terry_hk
ID: 10837263
I want to know why only "a" can retrieve the answer..otherwise I can't test the program.
It always returns null, I can't test it.
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10837608
If you really really want something quick for this, you can make phonebook static or the whole HashTable static...  NOTE: this is just as a quick and dirty solution!
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10840074
Terry,

The solution is as simple as

class HashTable {
     private Map phonebook = new HashMap();

  public void addEntry(Integer a, String b) {

    phonebook.put(a.toString(), b);
    System.out.println(a+"\n"+b);
  }
    public String getEntry(Integer c) {
         return (String)phonebook.get(c.toString());
  }
}

Since Integer is having a reference to the memory, each time you create, the reference would change.  (The hashCode in the Object)

So that is the reason why you were not able to get the value back

Put the key as String, String doesn't have any reference.  So you can use that to get the value back.

Hope this helps you.


Regards,
Muruga
0
 

Author Comment

by:Terry_hk
ID: 10840856
The id will be in string form after changed?
Can it store as integer?
Because I want to view all assets maintained, and sorted by ID later.


0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10840877
Changing to Integer would be tough case to retrieve back.

You can store it is String when retrieving, get the string and change it Integer.

You can maintain another Map where you can store it as Integer.  So that you can use that Map for sorting purpose.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10841723
>>Since Integer is having a reference to the memory, each time you create, the reference would change.

Please explain (bearing in mind that the original problem was that null was being returned)
0
 

Author Comment

by:Terry_hk
ID: 10842253
Muruga's method is work! I can retrieve the data from hash table now. And I modify my program as following:

public class AssestTrackerUI extends JFrame {
  ElectronicApplicance ea=new ElectronicApplicance();
  Automobile am=new Automobile();


  void butSearch_actionPerformed(ActionEvent e) {
   taSearch.setText(ea.idsearch(Integer.parseInt(txtSearch.getText())));
   }

  void butEnter_actionPerformed(ActionEvent e) {
  if(jComboBox1.getSelectedIndex()==0){
   ea.set(id,txtEnter.getText(),taEnter.getText());
   ea.asset(jTextField7.getText());
   }
       id=id+1;
       }
  }
}

Each asset has a hash map now.
According to the exercise requirement, I should provide one hash map shared by all of the assets.Or each asset has a hash map?

If each asset has a hash map, then I need search all of the hash map when I want to retrieve the data.The program will be much difficult...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10842333
>>I can retrieve the data from hash table now

The reason for that is quite simple -  you have done what i suggested earlier:

>>
public class AssestTrackerUI extends JFrame {
  ElectronicApplicance ea=new ElectronicApplicance();
  Automobile am=new Automobile();
>>

i.e. you have stopped creating a hash map every time an event is handled and instead created just one. You've listened to my suggestions to a certain extent, but your program is still wrong. As i mentioned above, it is NOT the entities such as ElectronicApplicance and Automobile that should contain hash maps, but the AssetTracker

0
 

Author Comment

by:Terry_hk
ID: 10843904
I should declare "HashTable has=new HashTable();" in ElectronicApplicance...?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10843934
No
0
 

Author Comment

by:Terry_hk
ID: 10844414
I don't know how to correct it >_<

And I correct the calss name...


AssetTracker.java
class AssetTracker {
     private Map map = new HashMap();

  public void addEntry(Integer a, String b) {

    map.put(a.toString(),b);
  }
  }

Asset.java
abstract class Asset{
   AssetTracker has=new AssetTracker();
   public abstract void asset(String add);
   
    public void insert(){
       has.addEntry(new Integer(Id),name);
       has.addEntry2(name,Owner);
      }
      }

ElectronicApplicance.java
class ElectronicApplicance extends Asset{

    public ElectronicApplicance( ) {
 
    }
    public void asset(String add){
            Add=add;
            name=Add;
            insert();
            }
                  

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10844492
That's getting more like it, but you shouldn't be creating a *new* AssetTracker for each Asset subclass. A reference to a single existing AssetTracker instance should be passed to each Asset
0
 

Author Comment

by:Terry_hk
ID: 10844698
In AssestTrackerUI, I created 2 types of assets. It creat 2 Asset and 2 mpa.
ElectronicApplicance ea=new ElectronicApplicance();
Automobile am=new Automobile();

How can I creat one Asset and one map..?
I tried declare "Assest assest=new Assest();"...but abstract class can't use this code..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10844754
You should give each Asset subclass a register method. I would declare the following in Asset

public abstract void register(AssetTracker assetTracker);

The compiler will lead you in the right direction after making that alteration
0
 

Author Comment

by:Terry_hk
ID: 10845017
And then @@"....?

abstract class Asset{
  AssetTracker has=new AssetTracker();
  public void insert(){
  has.addEntry(new Integer(Id),name);
 }
  public abstract void register(AssetTracker assetTracker);
  public abstract void asset(String add);
}

class ElectronicApplicance extends Asset{
  public void register(AssetTracker assetTracker){

  }
   public void asset(String add){
      Add=add;
      name=Add;
      insert();
   }
}



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10845081
>>
abstract class Asset{
  AssetTracker has=new AssetTracker();
>>

No - each Asset should NOT create an AssetTracker. I don't know how many times I have to say this, but there should be just ONE AssetTracker. This should probably be owned by the AssetTrackerUI. Just to make it absolutely explicit what's going on, change

>>public abstract void register(AssetTracker assetTracker);

to

public abstract void registerAssetWithTracker(AssetTracker assetTracker);
0
 

Author Comment

by:Terry_hk
ID: 10845196
public class AssestTrackerUI extends JFrame {
       AssetTracker assetTracker=new AssetTracker();
       ElectronicApplicance ea=new ElectronicApplicance();
      Automobile am=new Automobile();

}


abstract class Asset{

  public abstract void registerAssetWithTracker(AssetTracker assetTracker);
  public abstract void asset(String add);
 
public void insert(){
  assetTracker.addEntry(new Integer(Id),name);
  assetTracker.addEntry2(name,Owner);
 }
}

class ElectronicApplicance extends Asset{
       public abstract void registerAssetWithTracker(AssetTracker assetTracker){
      }      
}            
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10845472
You need to do some more thinking. Don't just produce code without thinking about it. Why would registerAssetWithTracker be abstract in ElectronicApplicance? That should be something more like:

public void registerAssetWithTracker(AssetTracker assetTracker) {
    assetTracker.registerAsset(this.id); // Add my id to the AssetTracker's hash map
}    




0
 

Author Comment

by:Terry_hk
ID: 10848483
I dont know how to call insert >_<

abstract class Asset{
public void insert(AssetTracker assetTracker){
      assetTracker.addEntry(new Integer(Id),name);
}
}

class ElectronicApplicance extends Asset{
public void asset(String add){
      Add=add;
      nameAdd;
      super.insert();
}
}
0
 

Author Comment

by:Terry_hk
ID: 10848599
I get it !!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10848971
I hope so! You don't need insert - that's now done with registerAssetWithTracker. And you don't need that asset method either. Any business product should have an id, so even if there is only one constructor there should be something like this

public abstract class Asset {
    private int id;

    public Asset(int id) {
        this.id = id;
    }
}

You should then something like this in your program

ElectronicApplicance television = new ElectronicApplicance(1001);
television.registerAssetWithTracker(assetTracker); (assetTracker is owned by AssetTrackerUI)

How would registerAssetWithTracker be implemented? Something like this:

public void registerAssetWithTracker(AssetTracker assetTracker) {
    assetTracker.register(new Integer(this.id), this); // register calls put on its hash map with these params
}



0
 

Author Comment

by:Terry_hk
ID: 10849589
If I want to delete an asset, I should set an id and call AssetTracker's delete in class Asset.
Or call AssestTracker's delete in AssestTrackerUI directly?

Thanks~

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849650
>>Or call AssestTracker's delete in AssestTrackerUI directly?

That's the one

0
 

Author Comment

by:Terry_hk
ID: 10850003
I want AssetTracker retuen all of the asset's id. I input 3 assets,so there id are 1,2 and 3.But the program always returns a wrong order..why?
I want it returns 1,2,3.

  public String viewall(){
        Iterator itor=map.keySet().iterator();
        while(itor.hasNext()) {
    String all;
    String key = (String)itor.next();
    String val = (String)map.get(key);
        all="Tracking ID: "+key+"\n";
        strBuf.append(all);
        }
        return strBuf.toString();
        }



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10850034
Maps are not ordered by key unless you use a TreeMap
0
 

Author Comment

by:Terry_hk
ID: 10850071
but I input it in order...It change my order @_@
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 10850108
It doesn't matter what order you input it in - the result is unordered in a Map
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10858755
8-)

Well done for turning that round. I hope you're well on your way now
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now