user5192032
user5192032

Reputation:

Line separator in Java

I am trying to print some values in new line but for some reason Java doesn't allow me to do it. Value for the second variable shows up in a new line but not for the third one. I've tried System.getProperty() and \n\n both gives me two new lines while I want just one line separator. Below is the code:

emailBody.append("Variable1: " + cu.getVariable1() + "\n");
emailBody.append("Variable2: " + cu.getVariable2() + "\n");
emailBody.append("Variable3: " + cu.getVariable3() + "\n");

On Console:

Variable1: 1

Variable2: 2 Variable3: 3

I am not sure why Variable3 does not go to the next line.

Thanks,

Upvotes: 1

Views: 453

Answers (1)

AlexWien
AlexWien

Reputation: 28727

I do it this way

private static final String LF = System.getProperty("line.separator");

emailBody.append("Variable1: " + cu.getVariable1() + LF);
emailBody.append("Variable2: " + cu.getVariable2() + LF);
emailBody.append("Variable3: " + cu.getVariable3() + LF);

If this does not work, then the problem is else where.

Upvotes: 5

Related Questions