Skip to content Skip to sidebar Skip to footer

How to Print on a New Line and Continue on Same Line Java

Printing on the same line in the console

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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.

"The Hood"

Posts: 8521

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • Quote
  • Report post to moderator

System.out.println() includes a newline action.
System.out.print() stays on the same line.

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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.

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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.

Bartender

Posts: 783

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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

    Number of slices to send:

    Optional 'thank-you' note:

  • 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.

tumbleweed

Posts: 5089

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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

    Number of slices to send:

    Optional 'thank-you' note:

  • Quote
  • Report post to moderator

YESSSS!!!
Finally I have Got It Right !!!
Thanks A LOT Peter Tran & Johannes de Jong

  • Mark post as helpful
  • send pies

    Number of slices to send:

    Optional 'thank-you' note:

  • 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

    Number of slices to send:

    Optional 'thank-you' note:

  • 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

    Number of slices to send:

    Optional 'thank-you' note:

  • 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

    Number of slices to send:

    Optional 'thank-you' note:

  • Quote
  • Report post to moderator

Thanks !!!

bergerphent1973.blogspot.com

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"