Posted on June 16, 2009 - by admin
Problem Accessing Hashtables From Another Class?
Hi everyone,
Very new to Java, I’m trying to access a set of Hashtables (in the Timeseries class) from my main method (in the Engine class).
but keep getting all kinds of error messages stating that:
myTimeseries cannot be resolved
and
Type mismatch: cannot convert from Timeseries to Hashtable
I dont know how to do it, could some one offer me a hand please.
I have not set any data get as I thought it a good idea to get this to work first
Many thanks
[code]
package StatPairsModel;
import java.util.Hashtable;
public class Engine {
public static void main(String[] arguments) {
Instrument stock1 = new Instrument();
Instrument stock2 = new Instrument();
int numberOfPairs = 3;
stock1.setStock("0015425", "Vodafone", "VOD LN", "UK", "TELECOM",
"GBP", "VOD LN", "FTSE");
stock2.setStock("0045125", "Telecom Italia", "TIM IM", "IT", "TELECOM",
"EUR", "TIM IM", "MIB");
int i=0;
for ( i = 0; i < myTimeseries.length; i++) {
//**Problem is on the next two lines of code**
Hashtable myTimeseries = new Timeseries();
myTimeseries.setTimeseries(mnemonic... myIdMapln, myDateMapln, myClose_price_euroMapln,
myClose_volumeMapln, myClose_market_capMapln, myCorp_action_indMapln,
myBetaMapln, myClose_price_localMapln);
}
}
}
package StatPairsModel;
import java.util.Hashtable;
public class Timeseries{
Hashtable mnemonicMap = new Hashtable();
Hashtable idMap = new Hashtable();
Hashtable dateMap = new Hashtable();
Hashtable close_price_euroMap = new Hashtable();
Hashtable close_volumeMap = new Hashtable();
Hashtable close_mcapMap = new Hashtable();
Hashtable close_corp_actionMap = new Hashtable();
Hashtable betaMap = new Hashtable();
Hashtable close_price_localMap = new Hashtable();
public void setTimeseries(Hashtable mnemonicMapln,Hashtable idMapln,Hashtable dateMapln,Hashtable close_price_euroMapln,
Hashtable close_volumeMapln,Hashtable close_mcapMapln,Hashtable close_corp_actionMapln,
Hashtable betaMapln,Hashtable close_price_localMapln) {
mnemonicMap = mnemonicMapln;
idMap = idMapln;
dateMap = dateMapln;
close_price_euroMap = close_price_euroMapln;
close_volumeMap = close_volumeMapln;
close_mcapMap = close_mcapMapln;
close_corp_actionMap = close_corp_actionMapln;
betaMap = betaMapln;
close_price_localMap = close_price_localMapln;
}
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;
}
public void setDate(Hashtable dateMapln) {
dateMap = dateMapln;
}
public Hashtable getDate(Hashtable dateMap) {
return dateMap;
}
public void setClose_price_euro(Hashtable close_price_euroMapln) {
close_price_euroMap = close_price_euroMapln;
}
public Hashtable getclose_price_euro(Hashtable close_price_euroln) {
return close_price_euroMap;
}
public void setClose_volume(Hashtable close_volumeMapln) {
close_volumeMap = close_volumeMapln;
}
public Hashtable getclose_volume(Hashtable close_volumeMapln) {
return close_volumeMap;
}
public void setClose_market_cap(Hashtable close_mcapMapln) {
close_mcapMap = close_mcapMapln;
}
public Hashtable getclose_market_cap(Hashtable close_market_capln) {
return close_mcapMap;
}
public void setCorp_action_ind(Hashtable close_corp_actionMapln) {
close_corp_actionMap = close_corp_actionMapln;
}
public Hashtable getcorp_action_ind(Hashtable corp_action_indln) {
return close_corp_actionMap;
}
public void setbeta(Hashtable betaMapln) {
betaMap = betaMapln;
}
public Hashtable getbeta(Hashtable betaln) {
return betaMap;
}
public void setclose_price_local(Hashtable close_price_localMapln) {
close_price_localMap = close_price_localMapln;
}
public Hashtable getclose_price_local(Hashtable close_price_localMapln) {
return close_price_localMap;
}
void display(int i) {
System.out.print(Timeseries.this.mne... + "\t");
System.out.print(Timeseries.this.idM... + "\t");
System.out.print(Timeseries.this.dat... + "\t");
System.out.print(Timeseries.this.clo... + "\t");
System.out.print(Timeseries.this.clo... + "\t");
System.out.print(Timeseries.this.clo... + "\t");
System.out.print(Timeseries.this.clo... + "\t");
System.out.print(Timeseries.this.bet... + "\t");
System.out.print(Timeseries.this.clo... + "\t");
System.out.println();
}
}
Visit My Website
June 17, 2009
Permalink
Type mismatch: cannot convert from Timeseries to Hashtable
should this line:-
Hashtable myTimeseries = new Timeseries();
read
Timeseries myTimeseries = new Timeseries();
Visit My Website
June 17, 2009
Permalink
i would answer your question properly if i could be bothered reading it all.
Visit My Website
June 17, 2009
Permalink
You’re not using Hastable correctly and you’re not using Generics which could save you a lot of lines of code. is the signature. So, I think you could do this… hashT = new Hashtable (); (); …
Hashtable
Hashtable
hashT.put(”Vodaphone”, new Instrument(”0015425″, “Vodafone”, “VOD LN”, “UK”, “TELECOM”,
“GBP”, “VOD LN”, “FTSE”);
Then if you want that value
hashT.get(”Vodaphone”).getCloseQuote()…
provided class Instrument is defined with all those fields and a method named getCloseQuote();
Using Generics, Hashtable
You get methods like
for( Instrument i : hashT) {
System.out.println( i.getOpenQuote() );
or if you wanted all the keys
String[] k = hashT.getKeys();
for(int i = 0; i< k; i++)
hashT.get( k[i] ).setSold( true );
there is no hashT.set(), just hashT.put(k,v);, hashT.removeAll();
hashT.addAll( arr[]); things like that.
go to the Java API for Hashtable. The methods would allow you to catagorize all your Objects under one Collection instead of the code you have done thus far.
Visit My Website
June 17, 2009
Permalink
I’m no java developer, but have a look at the following section of code…
for ( i = 0; i < myTimeseries.length; i++) {
//**Problem is on the next two lines of code**
Hashtable myTimeseries = new Timeseries();
You are declaring the myTimeseries object INSIDE the for loop, which is trying to use myTimeseries for its condition checking. declare a myTimeseries object before starting the for loop, and do not reassign myTimeseries within the for loop - thats asking for trouble unless you know what you are doing.
Visit My Website
June 17, 2009
Permalink
“I have not set any data get as I thought it a good idea to get this to work first”
I don’t know java but mabye you are getting an error because the data is undefined?
Try entering some test data and try again