• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to insert a new line character in a String?

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are new line characters inside the String as you expected. See this modified snippet.



produced

 
Ranch Hand
Posts: 70
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 80282
432
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiaber Shaam wrote: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 ]



I'm not that knowledgeable about the topic but to get familiar with it, I did some browsing. I hope the reference below will guide us a bit.

Reference : https://www.wyzant.com/resources/blogs/266636/embedding_line_breaks_in_java_literal_strings
 
Fire me boy! Cool, soothing, shameless self promotion:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic