Results 1 to 8 of 8
  1. #1
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default Array overwriting data

    Hi I have this program which stores ouputs of a function in an array. The output values are different each time I run the program. The problem is that everytime I ouput the data to files, I get the same ouput data. I want to get different outputs each time. I am writing the ouput to a text file 3 times.

    I have this loop below:
    Java Code:
    	for(int i=0; i<3;i++){
    		do{
    			d=t.f2(nsize);
    			System.out.println(d);
    			array1[count]=d;
    			nsize+=2;
    			if(nsize>=limit){
    				lim_reached = true;			
    			}
    			count++;
    				
    		}while(lim_reached==false);
    			run_no++;
    		writefile(fun_no,run_no);
    			
    		}

    Java Code:
    	
    	public static void writefile (int fun_no, int run_no){
    		
    		try{
    			BufferedWriter out = new BufferedWriter(new FileWriter("Function " + fun_no + " Run " + run_no +".txt"));
    			for(int i=0;i<array1.length;i++){
        			out.write(array1[i] + " ");
        			out.newLine();
        		}
        		out.close();
    
    		}catch(IOException e){
    			System.out.println("Error");
    			}
    	}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Can you show us the implementatio of method f2? I suspect that it returns the same object over and over again.

    kind regards,

    Jos

  3. #3
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    The methods for the functions are hidden. I need to work out the equations using the output data. Could it be that the for loop overwrites the data?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hobo View Post
    The methods for the functions are hidden. I need to work out the equations using the output data. Could it be that the for loop overwrites the data?
    No, that is impossible in your code; if method t.f2( ... ) returns the same object over and over again (most likely with different state) all elements of your array will point to the same object; that would (could) clairfy your problem.

    kind regards,

    Jos

  5. #5
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    Thing is if I get rid of the for loop and run the program 3 times the output data is this for the first run:
    0
    2
    3
    12
    18
    21
    46
    59
    56
    78
    109
    113
    143
    140
    247
    261
    320
    324
    340
    325
    398
    460
    553
    546
    511
    598
    761
    700
    920
    841
    849
    1023
    1074
    1073
    1258
    1234
    1273
    1479
    1594
    1520
    1504
    1574
    1625
    1909
    1959
    2200
    2184
    2230
    2233
    2496
    2407
    2431

    the values change. when I run it again and again.

    If I keep the for loop as it is then the output values for all the runs are the same.

    my array for storing the values is this : static long array1 [] = new long [100];

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hobo View Post
    Thing is if I get rid of the for loop and run the program 3 times the output data is this for the first run: [ snip ]
    the values change. when I run it again and again.

    If I keep the for loop as it is then the output values for all the runs are the same.

    my array for storing the values is this : static long array1 [] = new long [100];
    Now I see it: the return type of your method t.f2( ... ) is of type long, a primitive; the error is that you don't (re)initialize your variables 'count' and 'nsize' before that do ... while( ... ) loop starts so the second and third time around they have reached their final value without looping; add 'count= nsize= 0' before the 'do ...'

    kind regards,

    Jos

  7. #7
    hobo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default

    I added count=0 between the for loop and the do while but I still get the same output data in all 3 text files.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hobo View Post
    I added count=0 between the for loop and the do while but I still get the same output data in all 3 text files.
    Reread my reply again, I edited it: there are more variables you have to reset; also set the boolean lim_reached to false again. Better make that body of your for statement a separate method and do the work in there.

    kind regards,

    Jos

Similar Threads

  1. Import data to an Array
    By yrollgayanth in forum New To Java
    Replies: 8
    Last Post: 04-01-2010, 04:30 AM
  2. Replies: 0
    Last Post: 03-05-2010, 08:11 PM
  3. Array of different data types?
    By venkatteshb in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 06:42 PM
  4. add data into an array
    By mispeed in forum New To Java
    Replies: 9
    Last Post: 11-08-2007, 04:53 AM
  5. Add data to an array
    By adlb1300 in forum New To Java
    Replies: 8
    Last Post: 11-05-2007, 03:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •