Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a text file  RSS feed

 
Alex Breault
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would i complete the fileCreate method to write to a text file called "Results.txt" with this output?
819283761,15,100.0,30,A
834987341,9,60.0,18,D
899991828,13,86.66666666666667,26,B
888181818,9,60.0,18,D
892823736,6,40.0,12,F



 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Breault wrote:How would i complete the fileCreate method to write to a text file called "Results.txt" with this output?
819283761,15,100.0,30,A
834987341,9,60.0,18,D

I suggest looking in the Javadoc in the java.io package for appropriate classes to use. Specifically, character oriented classes and not byte oriented classes. And, of course, you should be looking for a class that supports output and not input. You may also want to keep an eye out for classes that buffer their data, not absolutely necessary, but more efficient.

There's also a lot of I/O tutorials available online.

Show us what you've come up with and let us know about any specific issues you're having.
 
Alex Breault
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is the method now but it does not save all info into the text file, just the last students
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because each time you call fileCreate(), you are creating a new file, so anything you wrote to the file the previous time you called fileCreate(), gets overwritten.

BTW, you might also want to check getLetterGrade(), some of your students might be very angry, after all, if they scored 89.91%, they would rightly expect to be awarded a B grade, and not a F grade.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would modify your fileCreate() to take a PrintWriter object. Actually, a better name for the method would be something like "fileWrite()". Then outside of your main loop, open up the PrintWriter, and inside your loop call fileWrite(). And don't forget to call close() when your loop completes.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a simple text file I would use a Formatter. Get rid of things like DecimalFormat; look on that as legacy code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!