Skip to content

Instantly share code, notes, and snippets.

@reddraggone9
Created April 26, 2015 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddraggone9/7ec41d7886fa48f11028 to your computer and use it in GitHub Desktop.
Save reddraggone9/7ec41d7886fa48f11028 to your computer and use it in GitHub Desktop.
A simple demonstration of a one-line, text progress bar in Java.
public class ProgressBar {
public static final char BLANK = '-';
public static final char FILL = '#';
public static final int TOTAL_TIME = 5000;
public static void main(String[] args) throws InterruptedException {
int length = 30;
if(args.length == 1) {
length = Integer.parseInt(args[0]) - 2;
}
int waitTime = TOTAL_TIME / length;
char[] progressBar = new char[length + 3];
progressBar[0] = '[';
progressBar[length + 1] = ']';
progressBar[length + 2] = '\r';
for (int i = 1; i <= length; i++) {
progressBar[i] = BLANK;
}
for (int i = 1; i <= length; i++) {
System.out.print(progressBar);
progressBar[i] = FILL;
Thread.sleep(waitTime);
}
System.out.println(progressBar);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment