Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding toString()

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I have a class called

student(name, age, address, class).

I need to do 2 operations( display and print to file)

I need to display using toString().
However, the problem comes when I need to print into file in this order
class + name + address + age.

Is there a profesional way to reuse the toString()?? instead of duplicate the code again?

Thanks alot
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't think so; in your case, the order of the variables being printed to screen is different from the order in which they should be written to the file (if I've understood your question correctly)

_steve.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Steve said, it appears that you require two different Strings (with different ordering of elements). So although these Strings are constructed in a similar manner, this isn't really a "duplication" of code -- it's 2 distinct things.
[ August 22, 2005: Message edited by: marc weber ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic