• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newline character

 
Katrina Owen
Sheriff
Pie
Posts: 1367
18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Does println add a newline character to the end of a line? That is, if I have a line that is 80 characters long, if I say println, will the line become 81 characters long, and therefore wrap on most Windows systems?

Katrina
 
Katrina Owen
Sheriff
Pie
Posts: 1367
18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, it looks like this is the case!

Katrina
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the API for PrintStream (System.out is a PrintStream object), the println() method will "Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n')."

However, the problem is that if your console is 80 characters wide and you write to the 80th character, many consoles will start a new line for you. Since you also start a new line, you get double spaced lines. The important thing is that different operating systems will behave differently, so don't depend on that behavior.
 
Katrina Owen
Sheriff
Pie
Posts: 1367
18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so basically I need to decide what I want to do and make sure that is the behaviour I get - no matter which operating system I am on.
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. Don't go and hard-code 80 (or 79 or 81) in your program because it's possible to reconfigure the Windows command prompt screen to have pretty much any width at all.
 
Katrina Owen
Sheriff
Pie
Posts: 1367
18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the xterm window i use, I can change the number of lines by changing the size of the window, but since the program specified that a line should be considered 80 characters long due to MS Windows defaults, that is what I was using.

Is there a cross-platform way of checking the number of characters a line will have?

Is there a point in making a

variable in a program this short where the value of line length is only used once?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic