Join 244,296 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 860 people online right now. Registration is fast and FREE... Join Now!
Hi, very very very new to java and need some help.. I have created a Hashtable in a class called Timeseries, and I would like to access these Hashtables in Another class called DataManipulation. But everything I try doesn't work. Could some one have a look at the code below and let me know how to do it please. I have taken out some code to make the listing more manageable.
Very many thanks Simon
java
package StatPairsModel;
public class TimeSeriesManiputaion {
void addIdTogether(int start,int finish){ int Total=0; for(int i = start; i<finish;i++){
//**I would like to access the getid hashtable from Timeseries here so I can do stuff with the data here //but i get the error ' The method getId(Hashtable) in the type Timeseries is not applicable for the arguments (String)' String newString = "1234" + i; Total=Total+Timeseries.getId(newString); } } }
java
package StatPairsModel;
import java.util.Hashtable;
public class Timeseries{
Hashtable mnemonicMap = new Hashtable(); Hashtable idMap = new Hashtable();
public void setTimeseries(Hashtable mnemonicMapln,Hashtable idMapln) {
mnemonicMap = mnemonicMapln; idMap = idMapln;
}
public void setMnemonic(Hashtable mnemonicMapln) { mnemonicMap = mnemonicMapln; }
public Hashtable getMnemonic(Hashtable mnemonicMap) { return mnemonicMap; }
public void setIdMap(Hashtable idMapln) { idMap = idMapln; }
public Hashtable getId(Hashtable idMap) { return idMap; }
Well right now the hash tables are private to your timeseries class and so you have two choices. Make them public by putting "public" on the front of their definitions and then you can access them directly through an instance of your timeseries class...
java
Timeseries t = new TimeSeries(); t.idMap.put("blahkey","blahvalue");
Or the better alternative is to only interact with your hash tables through public methods of the class. You have already begun doing this using methods like setIdMap() and getId() but instead of just returning or setting the whole hash table you would make a method like...
java
Timeseries t = new TimeSeries(); t.addToIdMap("blahkey","blahvalue");
... and then in the addToIdMap() method you would take in the parameters, evaluate them to make sure they are correct and then put them into the hash table directly because your methods of the class Timeseries can see, and access, the private hash tables you defined in that class.
Hope I am making a bit of sense to you here. You can find more information on what I am talking about by looking up the keyword "encapsulation" and "creating a public interface for your class".
Enjoy!
"At DIC we create public interfaces all the time... but if you were to look at us directly, our interface would probably scare you."
Well right now the hash tables are private to your timeseries class and so you have two choices. Make them public by putting "public" on the front of their definitions and then you can access them directly through an instance of your timeseries class...
java
Timeseries t = new TimeSeries(); t.idMap.put("blahkey","blahvalue");
Or the better alternative is to only interact with your hash tables through public methods of the class. You have already begun doing this using methods like setIdMap() and getId() but instead of just returning or setting the whole hash table you would make a method like...
java
Timeseries t = new TimeSeries(); t.addToIdMap("blahkey","blahvalue");
... and then in the addToIdMap() method you would take in the parameters, evaluate them to make sure they are correct and then put them into the hash table directly because your methods of the class Timeseries can see, and access, the private hash tables you defined in that class.
Hope I am making a bit of sense to you here. You can find more information on what I am talking about by looking up the keyword "encapsulation" and "creating a public interface for your class".
Enjoy!
"At DIC we create public interfaces all the time... but if you were to look at us directly, our interface would probably scare you."
Thanks for the reply. I'm really new to all this and am not sure how to make my hash table public (in order to access in another class), or how to finish writing a public method to interact with the class (or how to structure the call statement). I've been wrestling with this for days and can't get my head round it. Could you help by providing an example of how I do the above please.
Thanks for the reply, It may sound funny, but I'm still confused. This is how I have interpreted the last post: I create and set a few hashtables in the Timeseries class and try to query one of these tables via the Engine class, but it only returns a null I'm really struggling (I have a few books but no support or way of learning (that I can afford) other than you guys so any help is really deeply appreciated Thanks Simon
// If key is found, cast it to string and return // Otherwise return empty string. if (idMap.get(keyField) != null) { return (String) idMap.get(keyField); } return ""; }
So give that a try. If it says something about an improper cast, try using .toString on the get(). But notice how we can use the idMap directly as if it was defined in the function. This is because the method belongs to the class where the hashmap was defined. Methods of a class can see the class' member variables.
Thanks for your help, I can now pull data out of a hashtable when i put the query in the DataAccess class, but the last bit I would like to know is how do I access the hashtable from another class? I can extract the data from the Hashtable when I have the get statement in the DataAccess class, but I would like to do any data manipulation that I have to do in a class called Engine. Below is an attempt to get the Engine class to do this but I cannot not get the code to work i get cannot be resolved errors for the following line.