Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString

 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dianne Gerrelli
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, all you need to do is open a file, call toString(), write its contents to the file, and close the file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic