Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing on the same line in the console

 
ajay sagar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been stuck with this for a while.
Let me give you an example of what I am looking for :
if you have ever formatted a disk from the dos console you would remember that the percent completed would refresh on the same line.
I have been looking for code which does the same i.e. prints on the same line or rather updates that very line instead of printing on a new lineon every print call.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println() includes a newline action.
System.out.print() stays on the same line.
 
James Gray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.print() will make appending text possible, as noted above, but unfortunately Java's console streams don't yet make replacement possible.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always issue a cls command before doing print or println. It is probably as close to what you want as you can get using the console.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ajay,
You can use System.out.print() with a \r which does a carriage return without a line feed.
NOTE, this was tested on W2K.

-Peter
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works on Solaris too
Greetings
Karl
Originally posted by Peter Tran:

NOTE, this was tested on W2K.

-Peter

 
ajay sagar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it but it is not exactly what I am looking for.
I want to print on exactly the same place (position)where the previous String printed.
Try this :
Go to your dos console
put a empty/useless diskette in your floppy drive
format the diskette from the dos prompt
after a series of warnings you will get something like this
1%Completed,2%Completed...... printing on exacly the SAME location on the SAME line. I am desperate for a solution as I have to submit my project by monday and this is the only place I am getting stuck.
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajay have you tried removing the spaces between the /r and the number ie.
"System.out.print("\r 4") becomes System.out.print("\r4")
 
ajay sagar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YESSSS!!!
Finally I have Got It Right !!!
Thanks A LOT Peter Tran & Johannes de Jong
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this idea interesting and tried something too.
So, here's another one....
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jo,
Yo beat me to the answer. I thought my example would be a little clearer if I showed the spaces. The lesson here is not get cute. Just stick with the requirements!
-Peter
 
ajay sagar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am amazed by the number of ways the same task can be done in JAVA. I tried Shilpa Kulkarni's code and its amazing how she manages to get the same thing done.
Kudos to you Shilpa Kulkarni !!
its wonderful the way in which you put the idea from concept to code !
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic