Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print to the same line?

Tags:

java

I want to print a progress bar like so:

[#                    ] 1% [##                   ] 10% [##########           ] 50% 

But these should all be printed to the same line in the terminal instead of a new one. What I mean by that is that each new line should replace the previous, it's not about using print() instead of println().

How can I do that in Java?

like image 873
Aillyn Avatar asked Oct 29 '11 15:10

Aillyn


People also ask

How do I get my printer to print on the same line?

Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

How do you keep a print statement on the same line?

To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")

How do I make my printer not go to the next line?

Printing without a new line is simple in Python 3. In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') print("It is a great day.")

How do you print one line at a time in Python?

Use . readlines() . That code does print out line by line.


1 Answers

Format your string like so:

[#                    ] 1%\r 

Note the \r character. It is the so-called carriage return that will move the cursor back to the beginning of the line.

Finally, make sure you use

System.out.print() 

and not

System.out.println() 
like image 114
NPE Avatar answered Oct 09 '22 20:10

NPE