• Post Reply Bookmark Topic Watch Topic
  • New Topic

print formatting(what's the equivalent of \r in java ?)  RSS feed

 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I wrote this small program which serves as a stopwatch(Found at the bottom of the my post). What I want to see is, the time being printed(over-written) in the same line rather than in subsequent lines.

In C, I understand that this would be achieved using the "\r" as follows. How do I do the same in Java ?


My clock program is as follows


Thanks.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of the 0x000d char (\r) is not platform‑independent. I tried it a few months ago and got completely different results running Windows® from running Linux. Have you tried the backspace character \b?
Why don't you count down in seconds, using while (time > 0) which is better than while (true)? You can use time % 60 and time / 60 to display the time. Most of that code belongs in the clock, not the main method.
 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you tried the backspace character \b?

Thank you for the reply; I just tried the backspace character you mentioned and it works for me

Campbell Ritchie wrote:Most of that code belongs in the clock, not the main method.

I went through the link and found it very useful. I have rewritten my code accordingly. Please have a look and let me know what you think of it. I'm just starting off with Java and such advice really helps me adopt better coding standards from the beginning. Thanks once again.

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot better. But you still have a method doing lots of things. What about this for a loop?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise that printTime(seconds--) works like this:-
printTime(seconds); seconds--;   ?
It does not work like this:-
seconds--; printTime(seconds);
 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:A lot better. But you still have a method doing lots of things.

Thanks for that Sir. I shall try and make my methods smaller in future.

Campbell Ritchie wrote:You do realise that printTime(seconds--) works like this:-
printTime(seconds); seconds--;   ?

Yes, I understand post increment and shall use it as printTime(--seconds); to avoid printing seconds from 0.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aftab Hassan wrote: . . . Yes, I understand post increment and shall use it as printTime(--seconds); to avoid printing seconds from 0.
That isn't post‑increment. That's pre‑increment, and will ensure you print 0.
 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Aftab Hassan wrote: . . . Yes, I understand post increment and shall use it as printTime(--seconds); to avoid printing seconds from 0.
That isn't post‑increment. That's pre‑increment, and will ensure you print 0.

Yes, thanks Ritchie. My wording was ambiguous.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aftab Hassan wrote: . . . Yes, thanks Ritchie. My wording was ambiguous.
You're welcome

But your wording and programming were not ambiguous; they were both incorrect
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!