overriding toString

C

conrad

I have overriden toString in one of my classes
I craft a String object there much like this:
output = "Some text.\n"

output += "Some more text.\n";
output += "And some more text.\n";

return output;

In another class I create a PrintWriter
object and do the following for
an object of the class where I have
toString overriden:

printWriterObject.println(objectOfMyClass);

and upon looking at the text file that was
output all the new lines have been stripped
except the last that would correspond to
the use of println. Is this normal?
 
C

conrad

I have overriden toString in one of my classes
I craft a String object there much like this:
output = "Some text.\n"

output += "Some more text.\n";
output += "And some more text.\n";

return output;

In another class I create a PrintWriter
object and do the following for
an object of the class where I have
toString overriden:

printWriterObject.println(objectOfMyClass);

and upon looking at the text file that was
output all the new lines have been stripped
except the last that would correspond to
the use of println. Is this normal?


I should add that if I do not use a PrintWriter
object but instead send it to standard output,
then the output contains the newlines.
 
A

Alex.From.Ohio.Java

I should add that if I do not use a PrintWriter
object but instead send it to standard output,
then the output contains the newlines.

It looks like you spread logic of where to put new line in the string
to the different parts of your program.
Use KISS (keep it simple stupid) pattern.
Manage it all in one place (for example in toString() method) and
don't worry about it too much. Use print() method (not println) and
use \n in one place.

Point is that toString() or other methods shouldn't know or presume
where they are called. They shouldn't change their behavior or assume
what method call them.

Alex.
http://www.myjavaserver.com/~alexfromohio/
 
C

conrad

It looks like you spread logic of where to put new line in the string
to the different parts of your program.
Use KISS (keep it simple stupid) pattern.
Manage it all in one place (for example in toString() method) and
don't worry about it too much. Use print() method (not println) and
use \n in one place.

Point is that toString() or other methods shouldn't know or presume
where they are called. They shouldn't change their behavior or assume
what method call them.

Alex.http://www.myjavaserver.com/~alexfromohio/- Hide quoted text -


Just in case my post was not clear, I have
crafted up a small test case which demonstrates
my problem:

import java.io.*;

public class MyClass {
public static void main(String[] args) throws FileNotFoundException
{
MyClass foo = new MyClass();
System.out.println(foo);
File myFile = new File("c:\\java\\test.txt");
PrintWriter outputHandle = new PrintWriter(myFile);
outputHandle.println(foo);
outputHandle.close();
}
public String toString() {
String output = "Test 1\n";
output += "Test 2\n";
output += "Test 3\n";
return output;
}
}

sending the output to the standard output stream
produces the expected results.

Using PrintWriter to write it
to a file does not. Instead of
Test 1\nTest 2\nTest 3\n\n
written to a file, it is:
Test 1Test2 Test3\n

Why?
 
A

Alex.From.Ohio.Java

It looks like you spread logic of where to put new line in the string
to the different parts of your program.
Use KISS (keep it simple stupid) pattern.
Manage it all in one place (for example in toString() method) and
don't worry about it too much. Use print() method (not println) and
use \n in one place.
Point is that toString() or other methods shouldn't know or presume
where they are called. They shouldn't change their behavior or assume
what method call them.

Just in case my post was not clear, I have
crafted up a small test case which demonstrates
my problem:

import java.io.*;

public class MyClass {
public static void main(String[] args) throws FileNotFoundException
{
MyClass foo = new MyClass();
System.out.println(foo);
File myFile = new File("c:\\java\\test.txt");
PrintWriter outputHandle = new PrintWriter(myFile);
outputHandle.println(foo);
outputHandle.close();
}
public String toString() {
String output = "Test 1\n";
output += "Test 2\n";
output += "Test 3\n";
return output;
}

}

sending the output to the standard output stream
produces the expected results.

Using PrintWriter to write it
to a file does not. Instead of
Test 1\nTest 2\nTest 3\n\n
written to a file, it is:
Test 1Test2 Test3\n

Why?

as was mentioned in another post few minutes ago - open this output
file in wordpad (not notepad).

Alex.
http://www.myjavaserver.com/~alexfromohio/
 
C

conrad

On Apr 19, 1:58 pm, (e-mail address removed) wrote:
Just in case my post was not clear, I have
crafted up a small test case which demonstrates
my problem:
import java.io.*;
public class MyClass {
  public static void main(String[] args) throws FileNotFoundException
{
    MyClass foo = new MyClass();
         System.out.println(foo);
         File myFile = new File("c:\\java\\test.txt");
         PrintWriter outputHandle = new PrintWriter(myFile);
         outputHandle.println(foo);
         outputHandle.close();
  }
  public String toString() {
    String output = "Test 1\n";
         output += "Test 2\n";
         output += "Test 3\n";
         return output;
  }

sending the output to the standard output stream
produces the expected results.
Using PrintWriter to write it
to a file does not. Instead of
Test 1\nTest 2\nTest 3\n\n
written to a file, it is:
Test 1Test2 Test3\n

as was mentioned in another post few minutes ago - open this output
file in wordpad (not notepad).

What post was that mentioned in?

And why does this not work when using notepad?
What is the remedy for this?

This is pretty puzzling.
 
A

Arne Vajhøj

conrad said:
I have overriden toString in one of my classes
I craft a String object there much like this:
output = "Some text.\n"

output += "Some more text.\n";
output += "And some more text.\n";

return output;

In another class I create a PrintWriter
object and do the following for
an object of the class where I have
toString overriden:

printWriterObject.println(objectOfMyClass);

and upon looking at the text file that was
output all the new lines have been stripped
except the last that would correspond to
the use of println. Is this normal?

best advice: do not put newlines in toString output - it
messes up output rather unpredictable

second best advice: use System.getProperty("line.separator")

third best advice: use \r\n on Windows and \n on *nix

Arne
 
A

Alex.From.Ohio.Java

On Apr 19, 1:58 pm, (e-mail address removed) wrote:
I have overriden toString in one of my classes
I craft a String object there much like this:
output = "Some text.\n"
output += "Some more text.\n";
output += "And some more text.\n";
return output;
In another class I create a PrintWriter
object and do the following for
an object of the class where I have
toString overriden:
printWriterObject.println(objectOfMyClass);
and upon looking at the text file that was
output all the new lines have been stripped
except the last that would correspond to
the use of println. Is this normal?
I should add that if I do not use a PrintWriter
object but instead send it to standard output,
then the output contains the newlines.
--
conrad
It looks like you spread logic of where to put new line in the string
to the different parts of your program.
Use KISS (keep it simple stupid) pattern.
Manage it all in one place (for example in toString() method) and
don't worry about it too much. Use print() method (not println) and
use \n in one place.
Point is that toString() or other methods shouldn't know or presume
where they are called. They shouldn't change their behavior or assume
what method call them.
Alex.http://www.myjavaserver.com/~alexfromohio/-Hidequotedtext -
Just in case my post was not clear, I have
crafted up a small test case which demonstrates
my problem:
import java.io.*;
public class MyClass {
public static void main(String[] args) throws FileNotFoundException
{
MyClass foo = new MyClass();
System.out.println(foo);
File myFile = new File("c:\\java\\test.txt");
PrintWriter outputHandle = new PrintWriter(myFile);
outputHandle.println(foo);
outputHandle.close();
}
public String toString() {
String output = "Test 1\n";
output += "Test 2\n";
output += "Test 3\n";
return output;
}
}
sending the output to the standard output stream
produces the expected results.
Using PrintWriter to write it
to a file does not. Instead of
Test 1\nTest 2\nTest 3\n\n
written to a file, it is:
Test 1Test2 Test3\n
Why?
as was mentioned in another post few minutes ago - open this output
file in wordpad (not notepad).

What post was that mentioned in?

And why does this not work when using notepad?
What is the remedy for this?

This is pretty puzzling.

http://groups.google.com/group/comp.../2572d40eb4f7a0ed?hl=en&#doc_7e106870bfe511eb
Alex.
http://www.myjavaserver.com/~alexfromohio/
 
C

conrad

best advice: do not put newlines in toString output - it
messes up output rather unpredictable

second best advice: use System.getProperty("line.separator")

third best advice: use \r\n on Windows and \n on *nix

Arne-

Haha. I am admittedly used to programming on
Linux and was indeed bit by the fact that I
neglected how windows formats text files.

\r\n included and everything is now fine.

Much obliged.
 
A

Arne Vajhøj

conrad said:
Haha. I am admittedly used to programming on
Linux and was indeed bit by the fact that I
neglected how windows formats text files.

\r\n included and everything is now fine.

Much obliged.

Why not use System.getProperty("line.separator") so your code
is portable ??

Arne
 
M

Martin Gregorie

And why does this not work when using notepad?
What is the remedy for this?
Because notepad is confused unless newline is CRLF.
Wordpad is smarter than that and can handle newline = NL as well as CRLF.
 
M

Martin Gregorie

Because notepad is confused unless newline is CRLF.
Wordpad is smarter than that and can handle newline = NL as well as CRLF.
Oops: read LF for NL on the last line
 
C

Chase Preuninger

that's because \n means new line which works fine inside of java and
some systems, but many other systems use \r or \r\n. So what you
should do is use the System.getProperty("line.seperator") method to
return the correct one for the system you are on. (PrintWriter uses
this method to append the right new line character to the end of each
println() call.)

String newLine = System.getProperty("line.seperator");
String output = "";
output += "..." + newLine;

return output;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top