Results 1 to 8 of 8
Thread: Array overwriting data
- 10-27-2010, 09:44 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
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"); } }
- 10-27-2010, 09:57 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Can you show us the implementatio of method f2? I suspect that it returns the same object over and over again.
kind regards,
Jos
- 10-27-2010, 10:01 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
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?
- 10-27-2010, 10:23 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
- 10-27-2010, 10:31 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
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];
- 10-27-2010, 10:41 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-27-2010, 10:51 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 10
- Rep Power
- 0
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.
- 10-27-2010, 01:29 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Import data to an Array
By yrollgayanth in forum New To JavaReplies: 8Last Post: 04-01-2010, 03:30 AM -
Overwriting add() to make an own subclass of JDialog
By flobo in forum AWT / SwingReplies: 0Last Post: 03-05-2010, 07:11 PM -
Array of different data types?
By venkatteshb in forum New To JavaReplies: 1Last Post: 08-27-2008, 05:42 PM -
add data into an array
By mispeed in forum New To JavaReplies: 9Last Post: 11-08-2007, 03:53 AM -
Add data to an array
By adlb1300 in forum New To JavaReplies: 8Last Post: 11-05-2007, 02:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks