Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Would love some input  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot seem to get information to fully write to the employee.txt file. Suggestions would be fantastic. I've tried what is in our book but always seem to be missing something.


 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a method "public boolean addNewEmployee (Employee objEmployee)" which is intended to append data about an Employee to that file, but you don't ever call it. So I would expect that rather than not "fully" writing to the employee.txt file, that program wouldn't write anything at all to that file. Does that help, or would you like to clarify?
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And by the way, welcome to the Ranch!
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul has a point, the addNewEmployee(Employee) method isn't called. Don't know how you want it. Add one, write one or add a bunch (eg to a list) then write the bunch.

Also the addNewEmployee(Employee) is doing "print' rather than "write". If you call this method as it, the info gets printed to console, not to the file. Also should have a flush() at the end to actually write to file. And should have something in catch block.
 
Zuri Knapp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're completely right I am getting nothing to print to the employee.txt. I want to be able to add an unlimited number of employees either one at a time or multiple.

So to do this I will need to change the addNewEmployee to a "write" method and also call it. When I call it will I be calling it from a different class? Thank you I think this is a good start. I might need more clarification though on how to change it to a "write" method, can you think of any good examples I could look at?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start line 88-90 can change from print to write. This will output to file. Remember to add flush.

Then for where to call the addNewEmployee(Employee) method ... I think before or after you output the line "firstName lastName has been added" (line 73).

Base on the fact you will append basically forever, adding one at a time is best to avoid excessive memory usage (during write operation).
 
Zuri Knapp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That is part of a separate class titled employee... does it need to be a part of the main class? Isn't this what writes to the employee.txt file?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that method is the pat writing to file.

Given it's in a separate class, can you figure out how to call this method?

Edit: Why does it need to be in a separate class?
 
Zuri Knapp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't NEED to be in a separate file. It seems I'm not figuring out how to call the addNewEmployee and have it write to the file even though I seem to have the right code to write to the file. I simply have not figured out how to use them together.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Zuri Knapp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Great! Thank you. I will start working with that right now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!