How to display a Progress Bar on the Standard Console using Java

Arunprasadh C
Javarevisited
Published in
4 min readApr 27, 2022

Have you ever wondered about how a Progress Bar is shown on the Standard Console (especially when package managers like npm are used to download packages) ? I am sure that even if you have not wondered about it, you will love to learn about the same. Please note that this article won’t cover about tracking the progress of tasks or anything of that sort. Without any further ado, let us explore the solution.

First, let us create a Method called printMsgWithProgressBar() which accepts a String message to be printed, int length of the Progress Bar and long timeInterval which decides how fast the Animation should occur. Since we are going to call the Method from the static main() Method, we have to add static modifier to the printMsgWithProgressBar() Method. The Method would look like :

public static void printMsgWithProgressBar(String message, int length, long timeInterval){ }

We are going to use the StringBuilder class from Java to construct the Progress Bar as a String . You might wonder like how a String will look like a Progress Bar. Now, comes the Magical Part ;-). We are going to use two Unicode Characters :

  1. The U+2591 Unicode Character ░ for showing incomplete Progress.
  2. The U+2588 Unicode Character █ for showing completed Progress.

Do note that such characters might not be supported in Consoles which don’t support Unicode Characters. You can consider using other symbols like = or - for the Progress Bar characters.

We also need a StringBuilder instance for the purpose of building the Progress Bar. We are going to use the factory method generate() from the Java Stream API to generate the Progress Bar with the incomplete Unicode character. We can also use a simple loop for achieving the same. At this stage, your code should look like:

public static void printMsgWithProgressBar(String message, int length, long timeInterval)    
{
char incomplete = '░'; // U+2591 Unicode Character
char complete = '█'; // U+2588 Unicode Character StringBuilder builder = new StringBuilder();
Stream.generate(() -> incomplete).limit(length).forEach(builder::append);
}

Now, we need to print the message first before showing the Progress Bar. We have to declare a for loop with an int iterator that iterates from 0 till length-1 . Inside the loop, we have to replace the i th character of the builder with the complete block Unicode Character and print the Progress Bar. Now, the code should look like:

public static void printMsgWithProgressBar(String message, int length, long timeInterval)    
{
char incomplete = '░'; // U+2591 Unicode Character
char complete = '█'; // U+2588 Unicode Character StringBuilder builder = new StringBuilder();
Stream.generate(() -> incomplete).limit(length).forEach(builder::append);
for(int i = 0; i < length; i++)
{
builder.replace(i,i+1,String.valueOf(complete));
System.out.print(builder);
}
}

Now, if you execute this method using the main() method, it will NOT print a Progress Bar, but will instead print the String newly in each loop and it might even go to a new line. So, how to print the updated Progress Bar on the same line in each iteration ? The trick which we are going to use here is the Carriage Return \r Escape Sequence. It forces the Console to move the cursor to the starting of the line after printing the String. We have to append the \r Escape Sequence Character to the builder before printing it. Now, the code should look like:

public static void printMsgWithProgressBar(String message, int length, long timeInterval)    
{
char incomplete = '░'; // U+2591 Unicode Character
char complete = '█'; // U+2588 Unicode Character StringBuilder builder = new StringBuilder();
Stream.generate(() -> incomplete).limit(length).forEach(builder::append);
for(int i = 0; i < length; i++)
{
builder.replace(i,i+1,String.valueOf(complete));
String progressBar = "\r" + builder;
System.out.print(progressBar);
}
}

Now, if you run the code, you will get the Progress Bar on a single line, but it won’t look like an animation as there won’t be any delay in filling up of the Progress Bar. To add this delay in filling up, we are going to use the timeInterval value which we got as a method argument. We are going to pause the execution at regular intervals using Thread.sleep() method with timeInterval as its parameter.

That’s it ! Now, if you run the code, you will get an animated Progress Bar on the Console. Here’s the entire code:

Complete code for showing Progress Bar on the Console

The Progress Bar would look like this when it is incomplete:

Incomplete Progress Bar on the Console

After the Animation is over, it would look like:

Progress Bar with completed Progress on the Console

This is my first article on Medium.com ! I hope that you learnt something new with the help of this article. Thanks for reading this article ! Do follow me for more such interesting articles ! Have a nice day :-) !

--

--

Arunprasadh C
Javarevisited

I am an Android and Windows App Developer with mid-level Experience and an iOS App Developer with entry-level Experience, who is skilled in OO Design.