增加Alpha-Numeric值但限制为3个字符

时间:2016-06-06 06:07:12

标签: java

方案是 - 我读取文件的最后一行,将其递增1并将其写回。

已完成读写操作。我发现很难增加alpha-numberic值,因为它有一些条件。

条件是:

  • 它应该只有3个字符
  • 示例:A01,A02 .... A99,B01,B02 .... B99 ..
  • 一旦达到Z99,它应该是AA1,AA2,AA3 ...... AA9,.....
  • 然后AB1,AB2,... AZ9
  • 所以基本上增加值不应该是AA10,这使得它成为4个字符

我现在正在做的是分离字母和整数,递增它们并将它们连接起来。

到目前为止的代码:

String[] part = lastLine.split("(?<=\\D)(?=\\d)");
    System.out.println(part[0]);
    System.out.println(part[1]);    

int numberOnly = Integer.parseInt(lastLine.replaceAll("[^0-9]", ""));
numberOnly++;

String lettersOnly = lastLine.replaceAll("[^A-Z]", "");

if (lettersOnly.length() > 1){

            String lastLetter = lettersOnly.substring(lettersOnly.length() - 1);

            if(lastLetter.equalsIgnoreCase("Z") && number.equalsIgnoreCase("9") ){

                String notLastLetter = lettersOnly.substring(lettersOnly.length() - 2);
                char d = lettersOnly.charAt(0);
                d++;
                System.out.println("Letters after increment more tan two : " +d);
                lettersOnly = Character.toString(d) + "Z";
            }

        }
            else{

            }



        System.out.println("Letters after increment : " +lettersOnly);

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

如何从右到左分别递增每个“数字”并将溢出处理到下一个数字:

String number;//number - is your originally string
char[] digits = number.toCharArray();
boolean overflow = true;
for(int i = 2; i >= 0; i--){
    if(overflow){
        switch(digits[i]){
            case 'Z':
                digits[i] = '0';
                overflow = true;
                break;
            case '9':
                digits[i] = 'A';
                overflow = false;
                break;
            default:
                digits[i]++;
                overflow = false;
        }
    }
}
if(overflow){
    //handle ZZZ overflow here
}
String result = new String(digits);

答案 1 :(得分:1)

public class AlphaNumericCounter {
    String[] part;

    int counter; //Variable storing numeric part of counter

    String alpha; //Variable storing Alpha part of counter

    static String final_output = "A00"; // First Input considered as A00 and also the variable which will be store each count

    static boolean continueIncrement = true; //For running the loop till we reach ZZ9

    /* Def constructor */    
    public AlphaNumericCounter() {
    }

    /* Constructor called from main method with primary input A00 */
    public AlphaNumericCounter(String number) {
        part = number.split("(?<=\\D)(?=\\d)");
    }

    /* Function called each time from inside loop to generate next alphanumeric count */
    public void increment() {
        part = final_output.split("(?<=\\D)(?=\\d)");
        counter = Integer.valueOf(part[1]) + 1;
        alpha = part[0];
    }

    public String toString() {
        if (alpha.length() == 1){
            if (String.valueOf(counter).length() > 2){
                if ((int)alpha.charAt(0) + 1 > 90/*If Z encountered*/){
                    alpha = "AA";
                }else{
                    alpha = String.valueOf((char)((int)alpha.charAt(0) + 1));//Take Next Alphabet
                }
                counter = 1; //Reset counter to 1
            }
        }else{
            //We have AA, AB ... ZZ format of alpha
            if (String.valueOf(counter).length() > 1){
                if ((int)alpha.charAt(0) + 1 > 90 && (int)alpha.charAt(1) + 1 > 90){
                    continueIncrement = false;
                    System.out.println("NO MORE COMBINATION AVAILABLE"); //We reached ZZ
                    return "";
                }else if ((int)alpha.charAt(1) + 1 <= 90){
                    alpha = String.valueOf((char)((int)alpha.charAt(0))) + String.valueOf((char)((int)alpha.charAt(1) + 1));
                    counter = 1;
                }else if ((int)alpha.charAt(1) + 1 > 90){
                    if ((int)alpha.charAt(0) + 1 <= 90){
                        alpha = String.valueOf((char)((int)alpha.charAt(0) + 1)) + "A";
                        counter = 1;
                    }
                }
            }
        }

        generateString();

        return final_output;

    }

    private void generateString(){
        int l1 = String.valueOf(counter).length();
        int l2 = alpha.length();

        final_output = alpha + (l2 == 1 && l1 == 1 ? "0" : "") + String.valueOf(counter);
    }

    public static void main(String[] args) {
        AlphaNumericCounter lic = new AlphaNumericCounter(final_output);

        while (continueIncrement){
            lic.increment();
            System.out.println(lic);
        }
    }
}

答案 2 :(得分:0)

一个简单的解决方案是在Base36中计数

尝试一下:

class AlphaNumericIncrementer {
    public static void main(String[] args) {
        /*
        When starting at '000' => We hit 'zzz' (i.e. Dead End) at 46,656
        When starting at 'A00' => We hit 'zzz' (i.e. Dead End) at 33,696
        */

        int index = 0;
        String currentNumber = "000";
        while (index < 46656) {
            index++;

            String incrementedNumber = base36Incrementer(currentNumber, 36);
            currentNumber = incrementedNumber;

            if (incrementedNumber.toCharArray().length != 3) {
                System.out.println("We got intruder with length: " + incrementedNumber.toCharArray().length);
                System.out.println("Our Intruder is: " + incrementedNumber);
                break;
            }

            System.out.println(incrementedNumber);
        }
        System.out.println("Number of entries: " + index);
    }

    // The function that increments current string
    public static String base36Incrementer(String v, int targetBase) {
        String answer = Integer.toString(Integer.parseInt(v, targetBase) + 1, targetBase);

        return String.format("%3s", answer).replace(' ', '0');
    }
}