[Solved] Alphanumeric increment algorithm in JAVA [closed]


Here’s 3 solutions: the first two are somewhat arithmetic incrementations while the third is more a character manipulations.

The 3 implementations all pass the same unit tests:

 assertEquals("1DDA01A", MyClass.increment("1DDA00Z"));
 assertEquals("1A9AV00", MyClass.increment("1A9AU99"));
 assertEquals("AFH00", MyClass.increment("AFG99"));
 assertEquals("A2GF24", MyClass.increment("A2GF23"));
 assertEquals("ABAA0000", MyClass.increment("AAZZ9999"));
 assertEquals("11AB0A", MyClass.increment("11AA9Z"));

First:

public static String increment(String number) {
    Pattern compile = Pattern.compile("^(.*?)([9Z]*)$");
    Matcher matcher = compile.matcher(number);
    String left="";
    String right="";
    if(matcher.matches()){
         left = matcher.group(1);
         right = matcher.group(2);
    }
    number = !left.isEmpty() ? Long.toString(Long.parseLong(left, 36) + 1,36):"";
    number += right.replace("Z", "A").replace("9", "0");
    return number.toUpperCase();
}

Second:

public static String increment(String number) {
    Pattern compile = Pattern.compile("^(.*?)([0-9]*|[A-Z]*)$");
    Matcher matcher = compile.matcher(number);
    String remaining = number;
    String currentGroup = "";
    String result = "";

    boolean continueToNext = true;
        while (matcher.matches() && continueToNext) {
        remaining = matcher.group(1);
        currentGroup = matcher.group(2);
        int currentGroupLength = currentGroup.length();
        int base = currentGroup.matches("[0-9]*") ? 10 : 36;
        currentGroup = Long.toString(Long.parseLong("1" + currentGroup, base) + 1, base);  // The "1" if just to ensure that "000" doesn't become 0 (and thus losing the original string length)
        currentGroup = currentGroup.substring(currentGroup.length() - currentGroupLength, currentGroup.length());
        continueToNext = Long.valueOf(currentGroup, base) == 0;
        if (base == 36) {
            currentGroup = currentGroup.replace("0", "A");
        }

        result = currentGroup + result;
        matcher = compile.matcher(remaining);
    }

    result = remaining + result;
    return result.toUpperCase();
}

Third :

This works with your current “reqs”. Compared to how the question what asked at the beginning, this is not just a “left-part composed of letter” + “right part composed of digits”. Now, it’s “anything goes”, and letters roll from A to Z to A, while digits from 0 to 9 to 0. When a letter reaches Z, it is reset to A, then the digit/letter on its left is incremented.

If all numbers are incremented, it does not add a new digit on the left. You did not mention that in your question, but I’m sure you can figure this out from here:

public static String increment(String number) {
    char[] cars = number.toUpperCase().toCharArray();
    for (int i = cars.length - 1; i >= 0; i--) {
        if (cars[i] == 'Z') {
            cars[i] = 'A';
        } else if (cars[i] == '9') {
            cars[i] = '0';
        } else {
            cars[i]++;
            break;
        }
    }
    return String.valueOf(cars);
}

As for the “count”, your example isn’t sufficient to grasp the logic. Does it count only numbers ? what about the letters ? Does it follow a baseXx ?

how can AA010-AAA003 = 7, the 3 A’s versus 2 A’s do no matter ?
I feel this is rather on you to understand what are your requirements (ie: homework..)

Technically, this answers the question as it was asked originally (with many modifications along the way).

30

solved Alphanumeric increment algorithm in JAVA [closed]