Skip to content

Instantly share code, notes, and snippets.

@cicorias
Created October 11, 2020 13:26
Show Gist options
  • Save cicorias/4240bd5dc4bcffb978b20a76da5d8839 to your computer and use it in GitHub Desktop.
Save cicorias/4240bd5dc4bcffb978b20a76da5d8839 to your computer and use it in GitHub Desktop.
java progress bar system console
public static void progressPercentage(int remain, int total) {
if (remain > total) {
throw new IllegalArgumentException();
}
int maxBareSize = 10; // 10unit for 100%
int remainProcent = ((100 * remain) / total) / maxBareSize;
char defaultChar = '-';
String icon = "*";
String bare = new String(new char[maxBareSize]).replace('\0', defaultChar) + "]";
StringBuilder bareDone = new StringBuilder();
bareDone.append("[");
for (int i = 0; i < remainProcent; i++) {
bareDone.append(icon);
}
String bareRemain = bare.substring(remainProcent, bare.length());
System.out.print("\r" + bareDone + bareRemain + " " + remainProcent * 10 + "%");
if (remain == total) {
System.out.print("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment