File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Incrementing alphanumeric string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incrementing alphanumeric string" Watch "Incrementing alphanumeric string" New topic
Author

Incrementing alphanumeric string

Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
I have an assignment where I need to increment an alphanumeric string by one. For example AAA123 + 1 = AAA124. I'm okay with incrementing alpha or incrementing numbers, but incrementing them all together is where I am stuck. Should I just convert the entire string to ascii and increment there? Any nudge in the right direction will be appreciated.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40


Should I just convert the entire string to ascii and increment there?


Well, that depends on what the definition of "incrementing" is. Obviously, incrementing a "1" becomes "2", but what happens to a "9" or a "Z"? What is the ordering between the letters and numbers?

And what will cause the next character (the one next to the last one) to be incremented? Meaning what is the order that will cause an overflow?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
I'm sorry...I was not clear about what I meant by incrementing. I want to add one, so that AAA123 turns into AAA124. At first I considered incrementing the letters and numbers separately, but that will not help me in the case of AAA999 to AAB000. Clearly I need to treat the entire string as one number.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Not at first. First you try to increment the last character. If that would cause it to overflow, you increment the next, etc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Is the String supposed to represent a hexadecimal number?
Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
I'm not sure...I think ascii.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

Melinda Deering wrote:I'm not sure...I think ascii.


I highly doubt that it goes through all the ASCII characters -- there are many control characters in the ASCII character set, then incrementing it straight may yield strings that are not really text strings.

Henry
Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
Well, I would need to use 65-90 for upper case letters and 48-57 for numbers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
65-90? So you are using base 36 numbers? Or something like car registration numbers? Do you start from 1 and what happens after 9? Do you go on to 10 or A?

Do you remember when you were 7 years old and they taught you long addition at school? Can you remember about add 1, and carry 1?
Do you know how a several-number counter works? It is like long addition, only after getting to the top of the array you need the % operator to get back to the beginning.

Create a char[] array from 0 to 9 and A to Z, and a second 6-member array to hold the results.
Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
It's actually a project to increment license plate numbers, so AAA123 becomes AAA124.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Don't use 65-90 but instead 'A'-'Z'. They are actually the same, but the latter reads easier.
Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
I modified some code that someone shared with me that seems to do a similar kind of thing and I'll paste it below. It's not incrementing correctly, but I'm getting a tiny bit closer. I have commented where I believe I am going wrong. I want to be able to input a plate, and have it give me the next in sequence. (The instructor wants an input file, but I don't have any idea how to do those).

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

I modified some code that someone shared with me that seems to do a similar kind of thing and I'll paste it below.


When it comes to homework assignments, I am not a fan of this... you should do the assignment from scratch. There are two reasons for this...

1. You learn from the homework assignment. The more you figure it out yourself, the more you learn.

2. It is easier to fix something that you understand. The more code you borrow, the more code you may not understand, and hence, the more issues that you will encounter when you need to fix or modify the code.

Henry
Melinda Deering
Greenhorn

Joined: Apr 19, 2009
Posts: 11
Actually, I agree with you on this.

This is my third Java class. The first two classes were taught by a woman who had us write from scratch, of course, and gradually added on so all the parts made sense. This is my first assignment for a new course with an instructor I have never had before and I am in a total panic because he is requesting a number of concepts that I have never had any exposure to, such as ascii conversion, etc. My hope in posting is that someone will phrase something in a way that makes the concept stick for me....like converting characters to ascii.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
ASCII conversion is really easy if you know the values each char represents. Remember a char is really an unsigned integer in the range 0-65535 (approx) inclusive. So you can do arithmetic on them. You can find out the values if you go to the Unicode website; the "basic Latin" page has values in identical to ASCII. They are written in hexadecimal, so you have to forget about 'A'=65. It is now 'A'=0x41.

Write out the following pseudocode and implement it; it tells you how many marks you are going to get.
  • Take a String from the keyboard.
  • Apply the method of the String class which produces a char[]; I think it is called toCharArray().
  • Apply the % operator to each char: 'A' % 0x20 gives 1, 'B' % 0x20 gives 2, etc.
  • You get the same results for lower-case letters, but that only works for the 26 English letters. It won't work for accents or cedillas or similar.
  • 0x20 = space, so a space will count as a 0 when adding.
  • Add all the results together to get your final mark.
  • Note that the results of those calculations are always ints.
  • Try it with "hard work" and "knowledge" and see how far those will get you.

    Try this for loop, remembering the (char) cast is essential:
    You will now see you can create an array size '9' - '0' + 1 + 'z' - 'a' + 1. Remember 'z' - 'a' = 25. Better not to write 26, but let the computer do that arithmetic. Simple subtraction like that is a fast operation.

    Now: try this lot
  • Create a char[] array the size I told you earlier.
  • Create an index variable initialised to 0.
  • Set up a for-loop from '0' to '9'.
  • Each iteration does 2 things. It sets up the present member of the array (characters[index]) equal to the char for the loop.
  • The other thing it does is increments index by 1.
  • Set up a second for-llop from 'A' to 'Z' and work out for yourself what goes in that loop.
  • Print out all that array with the System.out.println() method and see what you have got
  • Work out a better way which doesn't duplicate the for loop.
  • Registration numbers usually only use capital letters.>
    G.Sathish kumar
    Ranch Hand

    Joined: Jul 27, 2009
    Posts: 84
    Melinda Deering wrote:I have an assignment where I need to increment an alphanumeric string by one. For example AAA123 + 1 = AAA124. I'm okay with incrementing alpha or incrementing numbers, but incrementing them all together is where I am stuck. Should I just convert the entire string to ascii and increment there? Any nudge in the right direction will be appreciated.


    i am not sure about the meaning of your add operation but try like get all last numberic like AAA123 in the sense take 123 and do the add operation and append it to the characters 'AAA'
    i dont think the ascii add process and all will be good solution.


    Thanks
    Sathish kumar
    SCJP, SCWCD
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38340
        
      23
    This is however an exercise in using arrays and counters. You can set up an array of 6 elements and use that array to access different letters or digits and print them like a number plate. The incrementing will then be done on a char[] array to find the next character. Agree that incrementing a String directly (probably impossible anyway) or even via a StringBuilder.

    By the way: What does n /= 26; mean? That doesn't look at all right. Do you mean %=?
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11229
        
      16

    Melinda Deering wrote:It's actually a project to increment license plate numbers, so AAA123 becomes AAA124.

    The question is...what happens when you increment AAA999?

    or really, what happens when you increment ANY 9 in any position?

    In my home state, license plates are always three letters followed by three numbers. so the next plate after AAA999 would be AAB000.

    However, in some states, it's possible that any position could be a letter OR a number, so the next plate after AAA999 would be AAA99A.

    How should it work in YOUR program?


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Melinda Deering
    Greenhorn

    Joined: Apr 19, 2009
    Posts: 11
    First of all, thanks to everyone who helped me out with this. This is what I ended up submitting:

    public class LicensePlate {

    public String nextPlate ( String s ) {
    int Num1 = 0, Num2 = 0, Num3 = 0, Ltr1 = 0, Ltr2 = 0, Ltr3 = 0;
    int add1, add2, add3, add4, add5, add6, carry1, carry2, carry3,
    carry4, carry5, carry6;

    String inputString ="";
    String nextPlate = "";
    inputString = s;
    // Process the first digit
    Num1 = (int)inputString.charAt(5)- 48;
    add1 = (char)(48 +(Num1+1)%10);
    carry1 = (char)(48 + (Num1 + 1)/10);


    // Process the second digit
    Num2 = (int)inputString.charAt(4)- 48;
    add2 = (char)(48 +(Num2+ (carry1 -48))%10);
    carry2 = (char)(48 + (Num2 + (carry1 -48))/10);


    // Process the third digit
    Num3 = (int)inputString.charAt(3)- 48;
    add3 = (char)(48 +(Num3+ (carry2 -48))%10);
    carry3 = (char)(48 + (Num3 + (carry2 -48))/10);


    // Process the first character
    Ltr1 =(char)inputString.charAt(2)- 65;
    add4 = (char)(65 + (Ltr1 + (carry3 -48))%26);
    carry4 = (char)(65 + (Ltr1 + (carry3 - 48))/26);


    // Process the second character
    Ltr2 =(char)inputString.charAt(1)- 65;
    add5 = (char)(65 + (Ltr2 + (carry4 -65))%26);
    carry5 = (char)(65 + (Ltr2 + (carry4 - 48))/26);


    // Process the third character
    Ltr3 =(char)inputString.charAt(0)- 65;
    add6 = (char)(65 + (Ltr3 + (carry5 -65))%26);
    carry6 = (char)(65 + (Ltr3 + (carry5 - 65))/26);


    nextPlate = ("" + (char)add6 + (char)add5 + (char)add4 + (char)add3 +
    (char)add2 + (char) add1);

    return nextPlate;
    }

    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19670
        
      18

    You did you leave out the code tags? You've shown back in April that you know how to use them, so why not now?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Incrementing alphanumeric string