Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By JosAH

Thread: Carriage return and new line

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Carriage return and new line

    Whats the difference between carriage return and new line ?
    Java Code:
    for (int i = 0; i < 100; i++) {
    	            System.out.print("Progress: " + i + " %\r");
    	            try {
    					Thread.sleep(100);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    	        }
    both print same results
    Java Code:
    for (int i = 0; i < 100; i++) {
    	            System.out.print("Progress: " + i + " %\n");
    	            try {
    					Thread.sleep(100);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    	        }
    and what is this Formfeed (0x0c) ?
    Last edited by allaudin; 06-28-2013 at 09:05 AM.

  2. #2
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Carriage return and new line

    This link will give you a thorough expanation
    Difference between '\n' and '\r' (SCJP forum at JavaRanch)

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Carriage return and new line

    not helpful at all :(

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: Carriage return and new line

    Think of an old typewriter: when you push the paper carriage to the right (so that those hammers print the letters on the left side of the paper), you 'return the carriage'; when you keep on pushing that bar, the carriage rotates a bit, so the paper exposes a 'new line'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Carriage return and new line

    you mean it always prints a new line and what about Formfeed ?
    Last edited by allaudin; 06-28-2013 at 08:50 PM.

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Carriage return and new line

    JosAH it returns the cursor to the begining of line ...
    Java Code:
    System.out.print("my name is allaudin \rHello !");
    if you run this code through command prompt it will print
    "Hello ! is allaudin"
    over-writing the first 9 words and cursor will be at index 10 ... if you write it will over write the "is allaudin".
    but in eclipse it works as "\n" (I DON'T KNOW WHY) ... now search and tell me why

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,328
    Rep Power
    5

    Default Re: Carriage return and new line

    It's a known behavior in Eclipse that \r does not behave like a traditional carriage return. So you can't continuously overwrite the same line in the Eclipse console by just printing a \r. I believe this was already discussed somewhere in this forum. If I find it I will post it.

    Regards,
    Jim
    allaudin likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: Carriage return and new line

    Quote Originally Posted by allaudin View Post
    JosAH it returns the cursor to the begining of line ...
    Tell me something new ... did you understand my typewriter analogy? Because that's where these terms come from.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Carriage return and new line

    you just said it will print new line ....

  10. #10
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Carriage return and new line

    ok jim ... post it here .. thanks

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: Carriage return and new line

    Quote Originally Posted by allaudin View Post
    you just said it will print new line ....
    This is what I wrote:

    Quote Originally Posted by JosAH
    Think of an old typewriter: when you push the paper carriage to the right (so that those hammers print the letters on the left side of the paper), you 'return the carriage'; when you keep on pushing that bar, the carriage rotates a bit, so the paper exposes a 'new line'.
    Tell us where I wrote that it will print a new line. I explained the difference between a carriage return and a new line character (see your own original question).

    Jos
    allaudin likes this.
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Carriage return and new line

    k I'm sorry ... I couldn't understand what you said ... my mistake ... chill ... Now I'm going to ask another one

  13. #13
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Carriage return and new line

    Ok just compile and run this piece of code... it will demonstrate the difference between the two

    Java Code:
    public class Just
    {
    	public static void main(String[] args)
    	{
    		System.out.println("What do you do?\r This is what I always do"); // it goes back to the begining of the line
    		System.out.println("");
    		System.out.println("What do you do?\n This is what I always do"); // it ges to a new line
    	}
    }

  14. #14
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Carriage return and new line

    Quote Originally Posted by allaudin View Post
    not helpful at all :(
    DId you even bother to read the entire thread on that link?

Similar Threads

  1. Java return line of the error
    By victoryo in forum New To Java
    Replies: 1
    Last Post: 01-13-2012, 06:48 PM
  2. Carriage Return Mark for JTextArea
    By dhany in forum AWT / Swing
    Replies: 5
    Last Post: 09-13-2011, 05:03 PM
  3. Replies: 4
    Last Post: 05-06-2011, 07:04 PM
  4. Waiting for a carriage return
    By Ebodee in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:46 AM
  5. New line or Carriage Return through FileWriter
    By johnt in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •