File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Printing on the same line in the console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing on the same line in the console" Watch "Printing on the same line in the console" New topic
Author

Printing on the same line in the console

ajay sagar
Greenhorn

Joined: Aug 23, 2001
Posts: 10
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

Joined: Sep 29, 2000
Posts: 8521
System.out.println() includes a newline action.
System.out.print() stays on the same line.


"JavaRanch, where the deer and the Certified play" - David O'Meara
James Gray
Ranch Hand

Joined: Sep 10, 2001
Posts: 30
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Jan 02, 2001
Posts: 783
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

Joined: Nov 22, 2008
Posts: 18944
It works on Solaris too
Greetings
Karl
Originally posted by Peter Tran:

NOTE, this was tested on W2K.

-Peter

ajay sagar
Greenhorn

Joined: Aug 23, 2001
Posts: 10
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

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Aug 23, 2001
Posts: 10
YESSSS!!!
Finally I have Got It Right !!!
Thanks A LOT Peter Tran & Johannes de Jong
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
I found this idea interesting and tried something too.
So, here's another one....
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
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

Joined: Aug 23, 2001
Posts: 10
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

Joined: Jun 07, 2000
Posts: 87
Thanks !!!
 
wood burning stoves
 
subject: Printing on the same line in the console