Reputation: 11744
I am trying to make a progress bar like this
[# ] 10%
[##### ] 50%
[#########] 100%
What i tried
public static void main(String[] args) throws InterruptedException {
String format = "[# ]%d%%\r";
for(int i=0;i<=100;i++){
System.out.print(String.format(format, i));
Thread.sleep(10);
}
}
output:
[# ] 10%
[# ] 50%
[# ] 100%
the problem is I can't increment the the #
count according to the progress.
So how can i move or increment the # ?
Upvotes: 2
Views: 3092
Reputation: 1312
I needed a progress bar for a project, I just put it on Github in case it can help somebody.
Basic idea is that your main task is divided into subtasks, you just notify the bar the advances of the subtask and the display is done automatically.
Sample usage with randomized timings:
ConsoleProgressBar bar = new ConsoleProgressBar(numberOfSubtasks);
bar.startAndDisplay();
for (int i = 0; i < numberOfSubtasks; i += increment) {
bar.updateAndDisplay(increment);
Thread.sleep(minrandomTime + rand.nextInt(maxrandomTime - minrandomTime));
}
Upvotes: 0
Reputation: 11744
All credits goes to @Poshemo
public static void main(String[] args) throws InterruptedException {
final StringBuilder sb = new StringBuilder();
String format = "[%-11s]%d%%\r";
for(int i=0;i<=100;i++){
if(i%10==0){
sb.append("#");
}
System.out.print(String.format(format, sb, i));
Thread.sleep(10);
}
}
Upvotes: 1
Reputation: 726549
You need to write a piece of code that produces a ten-character String
starting in #
s and ending in spaces. Pass this method a number from 0 to 100. The method should divide the number by ten, rounding the result up. This will give you the number of #
characters in the ten-character bar:
int numPounds = (pct + 9) / 10;
Make a loop that appends '#'
numPounds
times, and then appends ' '
until the length of the string is ten. Print the result between [
... ]
characters to complete the exercise.
private static final StringBuilder res = new StringBuilder();;
static String progress(int pct) {
res.delete(0, res.length());
int numPounds = (pct + 9) / 10;
for (int i = 0 ; i != numPounds ; i++) {
res.append('#');
}
while (res.length() != 10) {
res.append(' ');
}
return res.toString();
}
public static void main (String[] args) throws java.lang.Exception
{
for (int i = 0 ; i <= 100 ; i++) {
Thread.sleep(10);
System.out.print(String.format("[%s]%d%%\r", progress(i), i));
}
}
Upvotes: 2
Reputation: 1926
take a look at this
static int current=0;
static int previous=0;
static String previousString="";
public static void main(String[] args) throws InterruptedException {
String format = "[%s]%d%%\r";
for (int i = 0; i <= 100; i++) {
try {
current=i/10;
System.out.print(String.format(format, repeat("#",current ), i));
} catch (ArithmeticException e) {
System.out.print(String.format(format, repeat("#", 0), i));
}
Thread.sleep(10);
}
}
static String repeat(String StringToRepat, int repetition) {
if (repetition==previous)
{
return previousString;
}
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < repetition; i++)
builder.append(StringToRepat);
previousString=builder.toString();
previous=repetition;
return previousString;
}
Upvotes: 1