File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes regarding toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regarding toString()" Watch "regarding toString()" New topic

regarding toString()

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
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

Joined: Jul 17, 2005
Posts: 35

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)

marc weber

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: regarding toString()
It's not a secret anymore!