Java is resetting my variables to zero, and other mysterious things
Hi,
I'm not a great programmer, but I've spent a lot time with this problem and I would greatly appreciate any help.
So here's the idea:
I run load() from a class called menu. load() is located in a class called astock(). load() collects data from a file and loads it into an array called data of objects called stockdays. stockday is a class I wrote containing some doubles and integers.
Then while loading this data, I examine it to see if price changes on different days meet threshold values set in menu.
The crazy thing is, if I don't include the line: data[i].direction = 0; (marked by *** below) right before I run if-statements to examine price changes I get this behavoir:
each data[i].direction is equal to data[i-1].direction (the previously analyzed piece). For example, if the thresholds are met and the if-statement runs then data.direction is changed and so if every single one afterwards even though in my constructor for stockday I explicitly state direction = 0 (initialization really).
This problem is fixed if I include the ***'d line, but then I arrive at another problem.
In the next method run called posJumpEx() every single data[].direction is equal to zero.
I've written my stockday class with public variables. This may be bad form, I was just looking for a way to easily modify them. Could this be the problem?
I'm going to attach my code below, I apologize it's so long, but I just cant seem to find a way to just post the explanatory parts:
And it looks like all the tabs are screwed up, let me know if an email of the txt files would help. Thanks!
Code :
\\_____HERE is the stockday class________
public class stockday
{
public double
high,
low,
price;
public long
volume;
public int
direction; // 0 unassigned, 1 negative, 2 positive
public stockday (double fhigh, double flow, double fprice, long fvolume) //Constructor
{
high = fhigh;
low = flow;
price = fprice;
volume = fvolume;
direction = 0;
}
}
Code :
\\__________Here is the astock class, it uses the stockday objects ______________
public class astock
{
public String
ticker;
public static int
numDays;
public stockday
data[];
public double
pthresh,
nthresh,
pthrlen,
nthrlen;
public counter //Counts the pos and neg jumps
cpos,
cneg;
public astock(String fTicker, int fNumDays, double fpthresh, double fpthrlen, double fnthresh, double fnthrlen) { //Constructor
data = new stockday[fNumDays];
ticker = fTicker;
numDays = fNumDays;
pthresh = fpthresh;
pthrlen = fpthrlen;
nthresh = fnthresh;
nthrlen = fnthrlen;
stockday sd = new stockday(0,0,0,0);
for(int i=0; i <numDays; i++) //Need to initialize all stockday objects in array data[]
{
data[i] = sd;
}
cpos = new counter();
cneg = new counter();
}
public void load() { //Loads and marks jump locations
System.out.println(ticker);
counter c1 = new counter(); //Creates a counter
try {
FileInputStream gstream = new FileInputStream("C://Programming//StockData//" + ticker + ".txt");
DataInputStream stkIn = new DataInputStream(gstream);
String str;
double prevprice = 0; //yesterdays price
double pchg = 0; //percentage change from yesterday
for (int i = 0; i < numDays; i++) {
str = stkIn.readLine();
String fields[] = str.split(",");
data[i].price = Double.parseDouble(fields[0]);
data[i].high = Double.parseDouble(fields[3]);
data[i].low = Double.parseDouble(fields[2]);
data[i].volume = Long.parseLong(fields[1]);
//System.out.println("price: " + data[i].price + "\tVolume: " + data[i].volume + "\tLow: " + data[i].low + "\tHigh: " + data[i].high);
//Determine % chg, then set directions
********data[i].direction = 0; ********
if(i > 0) //Make sure we're not in the first run of the loop
{
pchg = (data[i].price - prevprice)/prevprice;
if( pchg < nthresh && pchg > (nthresh+nthrlen)) {
data[i].direction = 1;
cneg.click(); //add one to the counter to determine number of jumps
}
if( pchg > pthresh && pchg < (pthresh+pthrlen)) {
data[i].direction = 2;
cpos.click();
}
}
System.out.println("price change: " + pchg + "\tdirection: " + data[i].direction);
prevprice = data[i].price;
}
} catch (IOException e) {}
System.out.println("Number of Pos Jumps: " + cpos.get());
}
public void posJumpEx() { //Gets jump/correction magnitudes
double corrPerc[] = new double[cpos.get()+1];
int n=0;
for(int i=0; i < numDays; i++)
{
//System.out.println("Direction: " + data[i].direction);
if(data[i].direction == 2)
{
corrPerc[n] = (data[i+1].price - data[i].price)/data[i].price;
System.out.println("Correction %: " + corrPerc[n]);
n++;
}
}
}
Code :
\\___________Here is the menu class_____________ vary straightforward
public class menu {
private static double
pthresh = .08,
pthrlen = .07,
nthresh = -.08,
nthrlen = -.07;
private static int
numDays = 1249;
public static void main (String args[]) {
astock Fdata = new astock("CNX", numDays, pthresh, pthrlen, nthresh, nthrlen);
Fdata.load();
Fdata.posJumpEx();
}
}
Re: Java is resetting my variables to zero, and other mysterious things
schrosby
as you have suggested, its a bit difficult to read the files from here. can you attach the text files of the code
and the input file that is in ur c drive,
Moreover on glancing through the code your code looks fine. though i will need to run the program to see
can you explain the exact problem when you *** line is not there, as i dont see any benefits of having and it also means that your data set wll be wrong as well
1 Attachment(s)
Re: Java is resetting my variables to zero, and other mysterious things
Alright,
I've attached all the code and the output with and without the **** line. I've also attached the data file that the code reads in. You'll have to change the file references in the code to make it work properly though.
I'll try to do a better job explaining what happens.
I'm try to identify particular jumps in price by certain thresholds and I'm trying to identify them with a variable called direction.
direction = 0 for no jump, 1 for neg jump, and 2 for pos jump.
I initialize direction = 0 in the constructor for stockday so by default there is no jump unless assigned in the load() function in astock.
If I don't include that line and I examine direction I find that direction goes about as follows:
00000000002222222222221111111112222222222222222211 111111111111
where it should have been
00000000002000000000001000000002000000000000000010 0000000000000
I know that the if-statements are working because I get a reasonable number of counts via my counters
If I do include that **** line I get what it should be. So in a way everything is fixed. But including that line doesn't make sense and furthermore I find a new problem in my function posJumpEx() as I go to examine all the jumps that should have been recorded. If the line is included every direction variable is zero.
I realize this is hard to explain. I hope that what I've said helps. Thanks for taking a look!
Re: Java is resetting my variables to zero, and other mysterious things
Hi guys,
So I've done some more debugging and discovered what java is doing, but I still don't know why.
So what's happening is this:
in astock.load()
every time a new data element, data[i] is assigned a new value, every data[i] is assigned that value as well.
For example, say i = 2,
data[i].price = Double.parseDouble(fields[0]);
what happens is instead of just data[2].price = 45.67, every price element in data[] is equal to 45.67
I'm not familiar enough with java to figure out what this is happening. help would be most welcomed