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?
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.
To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")
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.")
Use . readlines() . That code does print out line by line.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With