• Post Reply Bookmark Topic Watch Topic
  • New Topic

Carriage Return "\r" character working like new line "\n" character on MacBook. Any tips ?  RSS feed

 
Lakshman Subramanya
Greenhorn
Posts: 5
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I use Eclipse (kepler version, with JDK 7) on MacBook Pro (Lion)

I want to overwrite a line printed on my console. I used "\r" for that. I was expecting the cursor to go to the beginning of the line. Instead, it is going to the next line.
Could some one tell me how to rectify this problem ?

Code:
------

System.out.print("Superman\rSober");


Expected Output:
----------------
Soberman


Actual Output:
----------------
Superman
Sober

Any help appreciated.

 
Ashwin Raghavan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure if i understood you correct. currently your output is

superman
sober

and you want it to become
Soberman

is that correct?
 
Lakshman Subramanya
Greenhorn
Posts: 5
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Thats right.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How old is your Mac? Macs used to use \r as their line end, but for the last few years they have changed to \n
The behaviour of \r is platform‑dependent. If I try…it will print greatell on this PC running Windows® and running Linux on the same machine I get a blank line.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshman Subramanya wrote:Could some one tell me how to rectify this problem ?

Simple answer: with custom code.

As Campbell has already pointed out, the behaviour of '\r' is platform-dependant and not necessarily what you want. You could, however, emulate it with something like:I should add that this is only ONE way of doing it. There are plenty of others. you should also make sure that 'line' is in fact a single line before you call it because, on a Windows system, a multi-line String could contain '\r's that are part of the CRLF pattern.

HIH

Winston
 
Lakshman Subramanya
Greenhorn
Posts: 5
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston, Campbell,
Thanks for the response and the alternate idea.

I figured out \r and \b behaves incorrectly when the program is ran through Eclipse IDE and its console on Mac OSX. It works fine when I run it through Mac Terminal app (command line).

Example:

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshman Subramanya wrote:Hi Winston, Campbell,
Thanks for the response and the alternate idea.
You're welcome
. . . \r and \b behaves incorrectly when the program is ran through Eclipse IDE and its console on Mac OSX.
. . .
What do yoiu mean by “behaves incorrectly”? Have you found anywhere that defines the behaviour of \r and \b? You mean it behaves differently. I do not know how the console on Eclipse handles those characters, and not having a Mac I don't think I can try it and find out.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshman Subramanya wrote:I figured out \r and \b behaves incorrectly when the program is ran through Eclipse IDE and its console on Mac OSX. It works fine when I run it through Mac Terminal app (command line).

My worry here is that your program is likely to be very platform-specific. Indeed, yours is likely to become terminal-specific, which is arguably even worse.

The problem is that you may be able to emulate the overlay characteristics you want, but cursor placement is another thing completely. For example, on many terminals '\b' may NOT necessarily be destructive, so
System.out.println("Hexagon\tOcta\b\b\b");
might well produce:
Hexagon Octa
(the underline shows where the cursor will be),
and
System.out.println("Hexagon\tOcta\b\b\bX");
something like:
Hexagon OXta

My point is that either:
(a) You need to decide what the display rules are.
(b) You need to choose a terminal that does what you want and document it in your program.

HIH

Winston
 
vikas byn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshman Subramanya wrote:Hello,

I use Eclipse (kepler version, with JDK 7) on MacBook Pro (Lion)

I want to overwrite a line printed on my console. I used "\r" for that. I was expecting the cursor to go to the beginning of the line. Instead, it is going to the next line.
Could some one tell me how to rectify this problem ?

Code:
------

System.out.print("Superman\rSober");


Expected Output:
----------------
Soberman


Actual Output:
----------------
Superman
Sober

Any help appreciated.




Its a bug in eclipse. Run your program on terminal/command promt
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know it is a bug?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last November, I wrote: . . . . . .
I think that should really be
 
John Canon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How do you know it is a bug?


https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936 (bug first reported in 2004, still open ...)

:-)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!