aspose file tools*
The moose likes Beginning Java and the fly likes toString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString" Watch "toString" New topic
Author

toString

Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
I would be grateful if someone could explain how the method toString works. I have been asked to write a method

void writeToFile (String Filename)

which takes the result of the toString on a sequence object and writes it out to a named file.

I have seen file.toString() used in code but I think this might be specifying the file name using the result of toString


I have therefore been trying to work out how to call the result of toString and thing that one way may be simply to copy it into a String varialbe. Is the code below likely to work ?



Another way which is more direct might be to say if this.toString () contains text, pass the text to a file reader, but I am not sure of the syntax for this.

Best wishes
Dianne
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
toString() is a method inherited by all classes you create in Java. The default implementation give you the type of the class followed by a @ followed by a hex address. You can override the toString method in any class you create to return a String which will gives useful information regarding the instance that calls it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If I'm understanding this...

It sounds like the toString method is already implemented in the Sequence class to return a String representation of the instance. So getting the String is straight forward: All you need to do is call toString on the instance.

To verify what you're getting from the toString method, simply insert a println...

System.out.println("The String is: " + dna1.toString());

But it sounds like your task is really to provide the input/output code that writes this String to the indicated file. Is this right? If so, what code do you have so far?


"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
sscce.org
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks for the replies

Gary, you are correct when you say

"But it sounds like your task is really to provide the input/output code that writes this String to the indicated file."

The code for my toString method within my Sequence class gets a copy of the description and content of the sequence object.



I have tried your suggestion


The printed line is
The String is: the DNA description is >DNA1 the DNA content is ATGCAAGGCCATC

I think what I need to be able to do is to write which ever Sequence object is the current one to a file. I can set the name of the file to something like output.txt So I think I need to use

this.toString()

and pass it to a filewriter to allow the content of toString to be written to a file. Does this make sense or am I missing something?

Best wishes
Dianne
Martin Mathis
Ranch Hand

Joined: Dec 20, 2004
Posts: 45
Yeah, all you need to do is open a file, call toString(), write its contents to the file, and close the file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString