wood burning stoves*
The moose likes Beginning Java and the fly likes Strings with \n newline chars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings with \n newline chars" Watch "Strings with \n newline chars" New topic
Author

Strings with \n newline chars

Roseanna Jeanette
Greenhorn

Joined: Mar 15, 2013
Posts: 1
I have a method which is returning a String. Within the method it builds this string up by adding bits to it e.g. returnString = returnString + "XXXX" +"\n";
This works fine and within the method I can print it out in full. However when I assign this value to the return value string and try and print it from outside the method, it only prints one line of the string. I have tried instead making the method return an array of Strings but that complicates it for other parts of the program so I really want to keep it as a String. Any ideas on how I can get the entirety of the String to be printed on multiple lines from outside the method?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Could you please post your code?


Swastik
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Roseanna Jeanette wrote:Any ideas on how I can get the entirety of the String to be printed on multiple lines from outside the method?

Yes. Don't use anything created by Microsoft (including the command window) to display it.

I suspect very strongly that what you're running into is the "MS experience" when it comes to text lines, because Windows (alone among desktop systems these days) uses the old CRLF style of ending a line.

However, Swastik is quite right. A bit of code would help us out a lot.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Winston Gutkowski wrote:I suspect very strongly that what you're running into is the "MS experience" when it comes to text lines, because Windows (alone among desktop systems these days) uses the old CRLF style of ending a line.


I know it is fun to spread Microsoft hate and blame them for everything, but using '\n' as a line separator is not what you are supposed to use - you are supposed to use System.lineSeparator() to be platform independent. Blaming MS for not being unix-based is not a solution.

Steve
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

In Java versions prior to Java SE 7, you have to use System.getProperty("line.separator")


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Steve Luke wrote:I know it is fun to spread Microsoft hate and blame them for everything, but using '\n' as a line separator is not what you are supposed to use - you are supposed to use System.lineSeparator() to be platform independent. Blaming MS for not being unix-based is not a solution.

Actually, I have to disagree there. Since we (still) know nothing of what Roseanna was trying to do - or even if she wrote the code herself - the only thing I can go on is the problem she described: not being able to see lines containing newlines displayed correctly.

Furthermore, this is a classic example of the reason for "Microsoft hate". They've had 30 years to fix the problem and do things the way that every other major desktop and intermediate system on the planet does (even Apple now), but they've chosen to ignore it.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Actually I learned CRLF as the way to terminate a line long before Microsoft was founded.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Actually I learned CRLF as the way to terminate a line long before Microsoft was founded.


Me too. On a typewriter. The carriage return lever pivoting on its hinge engaged gears that gave us the LF, and the return of the platen (carriage) back to the right-hand end of its travel gave us the CR (Although to be honest, MS may have existed at the point I learned this. I don't actually know when they were founded.)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Campbell Ritchie wrote:Actually I learned CRLF as the way to terminate a line long before Microsoft was founded.

Me three. But if it's for the same reason as Jeff, or because you remember line-printers, don't you think we should have got past this stupid anomaly - perpetuated only by the "hated" - by 2013?

Winston
 
GeeCON Prague 2014
 
subject: Strings with \n newline chars