0

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%
B.Simsek
  • 1,196
  • 1
  • 8
  • 15
  • Why do you have `while(true)` in `formatPercentage`? It's not needed – seqre Oct 29 '21 at 14:59
  • i have change it. – B.Simsek Oct 29 '21 at 15:02
  • 2
    Reading [Differences between System.out.println() and return in Java](https://stackoverflow.com/questions/25456472/differences-between-system-out-println-and-return-in-java) might help you, as your methods both print stuff themself as well as returning values that you then print again. You should rather decide on one of those (either have your methods print and not return, or have them not print and instead return). – OH GOD SPIDERS Oct 29 '21 at 15:02

2 Answers2

2

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;
}
WJS
  • 36,363
  • 4
  • 24
  • 39
0

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);
    }
}
Ryan
  • 1,762
  • 6
  • 11
  • Thank you for your work, but i can't work outside the method i got from school. I need to use their methods. – B.Simsek Oct 29 '21 at 16:36
  • And you can, I gave you this solution for a reason: You're still learning. The solution is in my code without explicitly spelling it out. – Ryan Oct 29 '21 at 18:28