Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How do I know when a carriage return is required?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 73733
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Screaming fools! It's nothing more than a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic