Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java is resetting my variables to zero, and other mysterious things

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!

    \\_____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;
    	}
    }
    \\__________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++;
    				}
     
    		}	
    	}
    \\___________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();
     
    	}
    }
    Last edited by helloworld922; January 8th, 2010 at 11:29 PM. Reason: Please use [code] tags!


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default 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

  3. The Following 2 Users Say Thank You to vkumar23 For This Useful Post:

    JavaPF (January 10th, 2010), schcrosby (January 12th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!
    Attached Files Attached Files

  5. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  2. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM
  3. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM