views:

636

answers:

7

Hello all, I'm stumped on how to convert 3 letters and 3 numbers to ascii and increment them by one...it's the old next-license-plate problem. Can anyone give me a nudge in the right direction?

A: 

I guess,

  char c='A';
  int no=97;
  System.out.println( (++c) + " " + (char)++no);
adatapost
A: 

You can do this by converting your String of letters and numbers to a char[]. Once you have done that you can iterate over the array and ++ each.

akf
A: 

You're making strings like this: "AAA000", "AAA001", ..., "AAA999", "AAB000", ..., "ZZZ999", right?

Think of it like a number system where the different columns don't use the same number of digits. So where our numbers are 10-10-10-10, your numbers are 26-26-26-10-10-10. Use an underlying integer which you increment, then convert to letters and digits by dividing and taking the modulo successively by 10, 10, 10, 26, 26, 26.

To convert a license plate to its underlying integer, multiply out the letter position (A == 0, B == 1, etc) by the proper power of 26, and the digits by the proper power of 10, and add them all together.

Ned Batchelder
+1  A: 
String str = "abc123"
String newstr = "";
for(int i=0; i<str.length(); i++) {
    newstr += (char) (str.charAt(i) + 1);
}
// newstr now is "bcd234"

Note that this does not handle the characters 'z','Z' or '9' the way you would want. But it should give you a start.

Also note that using StringBuilder to create newstr would be more efficient.

sepp2k
A: 

An easy way to generate plate numbers would be to have an integer variable which gets incremented and three integer variables corresponding to the letters, something like this, please modify where appropriate. One trick is to use String.format which seamlessly converts between an integer and its char counterpart (you can also use casts.)

class plateGenerator {
    int minLetter = "A".charAt(0);
    int maxLetter = "Z".charAt(0);
    int curLetter1 = minLetter;
    int curLetter2 = minLetter;
    int curLetter3 = minLetter;
    int number = 0;

    public String generatePlate() {
        String plate = String.format("%c%c%c-%03d",curLetter1,
                                curLetter2,curLetter3,number);
        increment();
        return plate;
    }
    private void increment() {
        number++;
        if (number == 1000) {
            number = 0;
            curLetter1++;
        }
        if (curLetter1 > maxLetter) {
            curLetter1 = minLetter;
            curLetter2++;
        }
        if (curLetter2 > maxLetter) {
            curLetter2 = minLetter;
            curLetter3++;
        }
        if (curLetter3 > maxLetter) {
            curLetter3 = minLetter;
            number++;
        }
    }

    public static void main(String[] args) {
        plateGenerator pg = new plateGenerator();
        for (int i = 0; i < 50000; i++) {
            System.out.println(pg.generatePlate());
        }
    }

}
Vinko Vrsalovic
+1  A: 

This problem actually have real applications. I wrote an account number generator that works just like this. I modified it to your format. Here you go,

public class LicenseNumber {

    int numericSum;
    int letterSum;

    public LicenseNumber() {
        numericSum = letterSum = 0;
    }

    public LicenseNumber(String number) {
        if (!number.matches("^[A-Za-z]{3}[0-9]{3}$"))
         throw new IllegalArgumentException("Number doesn't match license format");
        numericSum = Integer.valueOf(number.substring(3));
        letterSum = value(number, 0) * 26 * 26 + value(number, 1) * 26 + 
           value(number, 2);
    }

    public void increment() {
        increment(1);
    }

    public void increment(int inc) {
        numericSum += inc;
        if (numericSum >= 1000) {
         letterSum += numericSum/1000;
         numericSum %= 1000;     
        }
    }

    public String toString() {
        char[] letters = new char[3];
        int n = letterSum;
        for (int i = 0; i < 3; i++) {
         letters[2-i] = (char)('A' + (n%26));
         n /= 26;
        }
        return new String(letters) + String.format("%03d", numericSum);
    }

    private int value(String s, int index) {
        return Character.toUpperCase(s.charAt(index)) - 'A';
    }

/**
     * Example
     */
    public static void main(String[] args) {

     LicenseNumber lic = new LicenseNumber("ABC999");
     for (int i=0; i < 100; i++) {
         lic.increment(500);
         System.out.println(lic);
     }
    }

}
ZZ Coder
A: 

I haven't seen any code samples for general solutions for incrementing alphanumeric strings so I though I'd post mine.

This takes a string of any length with any ordering of alpha numeric characters, converts them to upper case and increments it by one (as if it were base 26). It also throws an exception if the numbers wrap. Its really up to you if wrapping makes sense...

private static string IncrementAlphaNumericString(string alphaNumericString)
{
    char[] an = alphaNumericString.ToCharArray();

    int i = an.Length - 1;
    while (true)
    {
     if (i <= 0)
      throw new Exception("Maxed out number!!!");

     an[i]++;

     if (an[i] - 1 == '9')
     {
      an[i] = 'A';
     }

     if (an[i] - 1 == 'Z')
     {
      an[i] = '0';
      i--;
      continue;
     }

     return new string(an);
    }
}
Michael La Voie