Skip to main content
1 of 3

Calculate different speeds between min and max speed with a stepsize

public static void main(String[] args) {
    int minKmh = 10;
    int maxKmh = 20;
    int step = 5;

    String allSpeeds = "";
    while (minKmh <= maxKmh) {
        allSpeeds += minKmh + ",";
        minKmh += step;
    }
    String[] allSpeedsAsArray = removeLastChar(allSpeeds).split(","); // result: 10,15,20

}

private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
}

So I have a minimum car speed and a max car speed. In this case 10 and 20. And I want every speed which is between min and max where min speed gets always incremented by step speed.

I dont't like my current version because it is large in my opinion. Is it posibble to shorten the code to 1-2 liner?