How to Print on a New Line and Continue on Same Line Java
Printing on the same line in the console
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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.
"The Hood"
Posts: 8521
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
System.out.print() will make appending text possible, as noted above, but unfortunately Java's console streams don't yet make replacement possible.
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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.
Bartender
Posts: 783
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
It works on Solaris too
Greetings
Karl
Originally posted by Peter Tran:
NOTE, this was tested on W2K.
-Peter
ajay sagar
Greenhorn
Posts: 10
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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.
tumbleweed
Posts: 5089
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
YESSSS!!!
Finally I have Got It Right !!!
Thanks A LOT Peter Tran & Johannes de Jong
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
I found this idea interesting and tried something too.
So, here's another one....
Peter Tran
Bartender
Posts: 783
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
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
posted 21 years ago Number of slices to send: Optional 'thank-you' note:
Thanks !!!
Source: https://coderanch.com/t/389825/java/Printing-line-console
Post a Comment for "How to Print on a New Line and Continue on Same Line Java"