• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I know when a carriage return is required?  RSS feed

 
Milin Tokes
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is a carriage return?

What's it's purpose?

What does it look like, \r?

keyboard.nextLine(); can also be used as a carriage return, why?

How do I know when a carriage return is required and when it isn't?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A carriage return is a character that instructs the terminal to move the cursor back to the beginning of the line. It is analogous to what you would do with a typewriter. As you typed, the carriage would move to the right so the next letter didn't overwrite the previous. When you reach the end of the line you pushed the carriage back to the beginning, and then moved the page up a bit so you were on a new line.

Confusingly new lines are represented differently on different operating systems. On windows it is a carriage return/line feed combination (\r\n), and on Unix systems it is just a line feed (\n). Java tries to hide this fact from you, so you should never need to worry about this difference. You do need to be careful when moving text files from a windows platform to a Unix platform though, because the line endings may be wrong.

You will probably never need to use a carriage return character directly because Java handles them for you and will put the appropriate line ending for the platform you are running on.

Can you give some context for what you are doing, and we can tell you the best way to do it.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

A long time ago I had to use a Teletype which had keys marked CR and LF.
You know when a carriage return is required because somebody asks you a question like
Line end type: CR LF CRLF (CRLF preferred)
Otherwise you simply forget that CR exists (the character rather than the character ‍) and use System.out.println(), System.out.printf("...%n...",...); or similar to end your lines.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!