I'm just wondering if there is a way to use System.out.println();other methods to create a cool loading bar when I run my program with a batch file.
The real question here is how can I make this bar as if it only prints on one line.
I do not want it to spread across multiple lines, as shown below:
[aaaaaccccccccccccccc] 25%
[aaaaaaaaaacccccccccc] 50%
[aaaaaaaaaaaaaaaccccc] 75%
He who stays clean in place will make things cleaner and friendlier.
Many thanks,
Justian
EDIT:
Ok I managed to find this link here: How to animate the command line? but the answer is:
- This is for C ++, not Java
- Seems not very efficient in Java, do I need to overlap every line in the loop?
Is there a better way to do this in Java?
EDIT:
Here is what I ended up with:
static final int PROGRESSBAR_LENGTH = 20;
public static void drawProgressBar(int numerator, int denominator) {
int percent = (int) (((double) numerator / (double) denominator) * 100);
String bar = "[";
int lines = round((PROGRESSBAR_LENGTH * numerator) / denominator);
int blanks = PROGRESSBAR_LENGTH - lines;
for (int i = 0; i < lines; i++)
bar += "|";
for (int i = 0; i < blanks; i++)
bar += " ";
bar += "] " + percent + "%";
System.out.print(bar + "\r");
}
private static int round(double dbl) {
int noDecimal = (int) dbl;
double decimal = dbl - noDecimal;
if (decimal >= 0.5)
return noDecimal + 1;
else
return noDecimal;
}
:
[||||||||||||||||....] 80% (. = )