This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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 ]
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!