Created
April 26, 2015 11:37
-
-
Save reddraggone9/7ec41d7886fa48f11028 to your computer and use it in GitHub Desktop.
A simple demonstration of a one-line, text progress bar in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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