• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem when export output to CSV file  RSS feed

 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I want to export output of my program as csv file. But the problem is that the data in my CSV when I export it going be static and just giving one value.
Example :
LongitudeLatitude
14731
14731
.........

I want the data in my CSV be dynamic and giving varian of values. Like this :
LongitudeLatitude
12731
103-6
.........

This is my code :

I think the problem is my array. Am I right?. Because when I try in fixed array it was succesfull. But when I try my code in above it just give me one result. I really tried in this case and stuck about it. Really need your help. Thanks
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your "variable" array will always be length 1 and contain only "data". Therefore your loop will only execute once.
Your BufferedWriter is not being opened for append, therefore all preceding values written in your "rect" loop will be overwritten.
 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried the array but it always show one result when I want to save it in csv
When I try to print the output of array in system.out.print it shows the contains of array but in separated results
Do you know how to get the dynamical value of array?
Because I've search it through google and a lot of forums but still dont get the answers
Please give me direction or clue
Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For every iteration of faceDetections.toArray() you are opening a writer to the same file and writing to it. This causes the contents to be overwritten and you end up with only one record
This is what Carey meant by
Carey Brown wrote:Your BufferedWriter is not being opened for append


Simple solution, tell the FileWriter you wish to append the contents.
How do you do it? Check out the constructors available for FileWriter

 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm so sorry if I was not understand about what Carey said before because English is not my main language
So there is any missunderstanding between us 
I just a beginner in java programming language. Please don't be angry to me 
So I have to create constructors in that class like what you said, Mr. Godbole?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need for sorry. Everyone struggles when learning a new language. You should have seen the mistakes I made when I started with Java

Junita Eka Sari wrote:So I have to create constructors in that class like what you said,

Yup. You need to use

instead of


As per the API docs,

public FileWriter(String fileName,
          boolean append)
           throws IOException
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
Parameters:
fileName - String The system-dependent filename.
append - boolean if true, then data will be written to the end of the file rather than the beginning

https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean)


 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I create new constructor I can't access variable lat and variable lng and do calculations to get value of variable lat and lng
That's why I create code export to csv in that loop
What is your opinion, Mr. Godbole?
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't have to create a new constructor, just add the argument "true" to the one you have.  Or am I misunderstanding what you mean by "create a new constructor"?
 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've add the arguments "true" and it exports all the lng value succesfull. But it still has one problem. The header "Longitude" also got loops. It should only has one row for header but when I add arguments "true" the header also got loops. How to fix it?
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this
 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Brown : The header still got iterations. I've fixed my code with add your arguments but the header still get iterations. I've fixed my code.


I think the mistakes in my array. Because when I print it give me results not like in one brackets like this [122,233,-3,...] but the result like this [122], [233], [-3], ..... I think that's why the header get iterations too. Am I right?
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration of "first" should be BEFORE the loop is even entered. Look at my suggestion more closely.

Move line 18 before line 1.
 
Junita Eka Sari
Greenhorn
Posts: 8
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've solved this problem. Thank you All 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!