• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Not able to write complete output to file

 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm trying to find out the minimum scalar product of a given two vectors. I've succeeded in my attempt to find out the minimum scalar product. I found the question here. I'm trying to solve the small input. My program calculates the minimum scalar product for all 1000 cases but is only writing 845 of them to the file. I don't understand what's the problem. I think it may have something to do with FileWriter. Any help is much appreciated.
My code is given below:
 
Ashwin Rao
Ranch Hand
Posts: 89
Python C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had forgotten to close the writer!!
After I added writer.close() I got the output! Sorry for the trouble!
 
Sheriff
Posts: 9024
658
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You still can improve your program, by:

1. Avoiding explicit imports on lines 1, 2.
2. By shortening your "main" method to a 1 or 2 lines in total.
3. By decomposing your code to a methods, which takes care of a single tasks.
 
Marshal
Posts: 80954
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you close the writer? Did you read the documentation for close() and did this explain your problem?
 
Ashwin Rao
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Closing the writer first flushes it and then closes it. So I guess it would have worked if I had just flushed it.
 
Campbell Ritchie
Marshal
Posts: 80954
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes and no. You would have had the file written had you simply called flush() but failing to close the writer can cause a resource leak.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic