aspose file tools
The moose likes Beginning Java and the fly likes How to insert a new line character in a String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to insert a new line character in a String?" Watch "How to insert a new line character in a String?" New topic
Author

How to insert a new line character in a String?

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Hi,

Please check the below snippet.

String problemNotes = new String();

problemNotes = "Date: "+childElement.getAttributeValue("date")+"\n"+"Submitter: "+childElement.getAttributeValue("submitter")+"\n"+"Notes: "+childElement.getText();

But i am not getting a new line at all.

Please help.
[ July 10, 2008: Message edited by: Shiaber Shaam ]

$Hi/\bEr
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
There are new line characters inside the String as you expected. See this modified snippet.



produced

Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Unless you are writing your String to a text file and you are a Windows user. Then the new line character depends on your OS (\n for Unix, \r\n for Windows and \r for old Macs) and you should use:


[ July 10, 2008: Message edited by: Rodrigo Tomita ]
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
This is an old thread, but I don't understand why Shiaber Shaam is not getting the expected new line in his output. Can anyone explain this? Thanks.
James Byrer
Greenhorn

Joined: Oct 05, 2010
Posts: 1
As Rodrigo said above, each OS has it's own "New line" character. You should use System.getProperty("line.separator") to automatically get the appropriate line separator. The "\n" he is using probably isn't working because he is running his app. on a Windows system.
Lukasz Jarocki
Greenhorn

Joined: Jan 01, 2013
Posts: 7
I have similar problem.
I am trying to read some text from .ini file and display it on JOptionPane confirm dialog. Here is the text in .ini file:



I supposed that each part of text will be placed in another line. But I was wrong. Whole string is in single line.


Surprisingly, I receive the same effect when I put string on stdout by System.out.println().
Here is the code:



Of course "sometext" appears in new line on console. How can I fix it?

Edit:
Ok I get it. I should use html.
http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
There is something wrong with that file you are reading, having \r\n in places and \n elsewhere. It looks as if it has been half written in Windows® and hal in Linux.
Try
String.format("This is a String%n%s is brilliant%n", name);
Or similar
Your \n worked on a JOptionPane dialogue on my Linux box.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to insert a new line character in a String?
 
Similar Threads
NullPointerException when run stand alone program
travel agent - java writing to file via user input
problem with toString() in Sun ONE Studio 4, update 1
unable to exit program..
How to set the text in JTextarea from bottom.