Line break by toString() in Java

drsbmm -  
 AL_UMPC -
Hello everyone,
well... I have a question, in Java:
I have the following class;
class Person {
private String lastName;
private String firstName;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String toString() {
return ("last name: " + lastName + "\n" + "first name: " + firstName);
}
}

In the JSP when I want to display an object of type Person using:
<%
Person per = new Person("dodo", "toto");
out.println("<h4>" + per + "</h4>");
%>

The problem is:
instead of the result being in the form:
last name: dodo
first name: toto
it displays as: "last name: dodo first name: toto " it ignores the line break

Please help me !!!
Configuration: Windows XP Internet Explorer 7.0

3 answers

danimo
 
Hello,

You can try:

String nL1 = System.getProperty("line.separator"); // for 1 line break
String nL2 = nL1 + nL1; // for 2 line breaks etc

and replace "\n" with + nL1

Best regards,

Dan
2