Home > Enterprise >  Progressbar come outside bracket
Progressbar come outside bracket

Time:10-30

i am making a progress bar for my school assignment. But when i run my code my progress bar come outside my bracket, but the = most be inside the bracket.

  public static String repeatString(int number, String str) {
        for (int i = 0; i < number; i  ) {
            System.out.print(str);            
        }
        return str;
    }

    public static String formatPercentage(int percentage) {
    if (percentage == 100) {
        return "done";
    }
    else{
        return percentage   "%";
    }
        
    }

    public static String formatBar(int percentage, int length) {
        int amount = percentage * length/ 100;
        int size = length - amount;
        return "["   repeatString(amount, "=")   repeatString(size, " ")   "] "   formatPercentage(percentage);
    }

this is the result:

          [= ] 5%
==        [= ] 20%
=======   [= ] 70%
==========[= ] done
==============      [= ] 70%

CodePudding user response:

Change your repeatString method to the following: Don't print anything here, just build up the string and return it.

public static String repeatString(int number, String str) {
        String pad = "";
        for (int i = 0; i < number; i  ) {
            pad  = str;
        }
        return pad;
}

CodePudding user response:

Try this:

public class ProgressBar {
    private int length;
    private int maxSteps;
    private int step;
    private char symbol;

    public ProgressBar(int length, int maxSteps, char symbol) {
        this.length = length;
        this.maxSteps = maxSteps;
        this.symbol = symbol;
    }
    
    public void increment() {
        increment(1);
    }
    
    public void increment(int numSteps) {
        step =numSteps;
        print();
    }
    
    private void print() {
        float percentage = (float)step/(float)maxSteps;
        int numSymbols = (int) Math.floor((double)length*percentage);
        StringBuilder builder = new StringBuilder();
        
        for (int i = 0; i < Math.min(numSymbols, length); i  ) {
            builder.append(symbol);
        }
        for (int i = Math.min(length, numSymbols); i < length; i  ) {
            builder.append(' ');
        }
        builder.append(String.format("[%s ]", symbol));
        
        if (numSymbols >= length) {
            builder.append(" done");
        } else {
            builder.append(String.format("%d %%", (int)(percentage*100)));
        }
        System.out.println(builder.toString());
    }
    
    public static void main(String [] args) {
        ProgressBar bar = new ProgressBar(10, 50, '=');
        bar.increment(10);
        bar.increment(15);
        bar.increment(20);
        bar.increment(5);
        System.out.println("Now exceeding max..");
        bar.increment(10);
        bar.increment(5);
    }
}

CodePudding user response:

Values that are larger or smaller than the value range produce undesirable effects in your example! Add a range check to your formatBar function to intercept values that are not in the range from 0 to 100.

For example:

public static String formatBar(int percentage, int length) {
    if(percentage < 0 || percentage > 100) return "Out of range: "   percentage;
    int amount = percentage * length/ 100;
    int size = length - amount;
    return "["    "=".repeat(amount)   " ".repeat(size)   "] "   (percentage == 100 ? "done" : percentage   "%");
}    
  •  Tags:  
  • java
  • Related