class TextTools {

    public static String advanceLastNumber(String originatingName) {


        if (originatingName.length() == 0) {
            return "1";
        }

        int lastDigitChar = -1;
        int firstDigitChar = -1;

        for (int i = originatingName.length() - 1; i >= 0; i--) {
            char c = originatingName.charAt(i);


            if (lastDigitChar == -1 && Character.isDigit(c)) {
                lastDigitChar = i;
            }

            if (Character.isDigit(c)) {
                firstDigitChar = i;
            }

            if (! Character.isDigit(c) && firstDigitChar > -1) {
                break;
            }
        }

        if (lastDigitChar == -1) {
            return originatingName + "1";
        } else {
            String oldDigitString = originatingName.substring(firstDigitChar, lastDigitChar + 1);
            Integer oldValue = Integer.parseInt(oldDigitString);
            Integer newValue = oldValue + 1;
            String newDigitString = newValue.toString();
            int lengthDifference = oldDigitString.length() - newDigitString.length();
            if (lengthDifference > 0) {
                String zeroPadding = new String(new char[lengthDifference]).replace("\0", "0");
                newDigitString = zeroPadding + newDigitString;
            }
            String firstPart = originatingName.substring(0, firstDigitChar);
            String lastPart = originatingName.substring(lastDigitChar + 1);
            return firstPart + newDigitString + lastPart;
        }
    }

}