Monster Monster - 1 year ago 64
Java Question

Console based progress in Java

Is there are easy way to implement a rolling percentage for a process in Java, to be displayed in the console? I have a percentage data type (double) I generated during a particular process, but can I force it to the console window and have it refresh, instead of just printing a new line for each new update to the percentage? I was thinking about pushing a cls and updating, because I'm working in a Windows environment, but I was hoping Java had some sort of built-in capability. All suggestions welcomed! Thanks!

Answer Source

You can print a carriage return \r to put the cursor back to the beginning of line.


public class ProgressDemo {
  static void updateProgress(double progressPercentage) {
    final int width = 50; // progress bar width in chars

    int i = 0;
    for (; i <= (int)(progressPercentage*width); i++) {
    for (; i < width; i++) {
      System.out.print(" ");

  public static void main(String[] args) {
    try {
      for (double progressPercentage = 0.0; progressPercentage < 1.0; progressPercentage += 0.01) {
    } catch (InterruptedException e) {}