• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 5
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Thats right.
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you know it is a bug?
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Last November, I wrote: . . . . . .

I think that should really be
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. And welcome to the Ranch
 
Happily living in the valley of the dried frogs with a few tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic