Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

Increment alphanumeric sequence

I am trying to create a method using the APEX language (Salesforce) that will increment a 3-digit alphanumeric sequence. Java is the closest to the APEX language, but it also share similarities with C#. I was hoping a JAVA or C# guru could point me in the right direction with the method to get the sequencing correct.

The sequence is as follows:

001 -> 999 (go thru all numeric)
A01 -> A99 (start alphanumeric)
B01 -> B99
.
.
.
Z01 -> Z99
AA1 -> AA9
BB1 -> BB9 
.
.
. 
ZZ1 -> ZZ9 (all alphanumeric utilized)
AAA -> AAZ (start all alpha)
BAA -> BAZ
.
.
.
ZAA -> ZZA
ZZB -> ZZZ (all alpha completed)
reset
001 (begin cycling thru sequence again)

Open in new window


I was trying to modify the following method to make it work, but am not having much luck

	public enum EMode {
    	AlphaNumeric, Alpha, Numeric
	}

	public String Increment(string text, EMode mode) {
		String retval = '';
    	String[] textArr = text.split('');
		textArr.remove(0);
		
    	// Add legal characters
    	List<String> characters = new List<String>();

    	if (mode == EMode.AlphaNumeric || mode == EMode.Numeric) {
        	for(Integer i = 0; i <= 9; i++) {
            	characters.add(String.valueOf(i));
        	}
    	}

    	if (mode == EMode.AlphaNumeric || mode == EMode.Alpha) {
        	for(String s = 'A'; s <= 'Z'; s++)
            	characters.add(s);
    	}

    	// Loop from end to beginning
    	for (Integer i = textArr.length - 1; i >= 0; i--)
    	{
        	if (textArr[i] == characters.Last())
        	{
            	//textArr[i] = characters.First();
            	retval = characters.First();
        	}
        	else
        	{
            	//textArr[i] = characters[characters.IndexOf(textArr[i]) + 1];
            	retval = characters[characters.IndexOf(textArr[i]) + 1];
            	break;
        	}
    	}
    	//return new string(textArr);
    	return retval;
	}

Open in new window


Unfortunately, APEX has limitations on the available libraries, so I'm working within the confines of the language and trying to use the available string methods: Apex String Methods

There is probably an easier way to accomplish the sequence perhaps using maps? Any and all suggestions are welcome.

Thanks in advance for any help.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Homework?
Avatar of -Dman100-

ASKER

Something like that...any ideas on how to accomplish an incrementing sequence like that?
any ideas on how to accomplish an incrementing sequence like that?

Well, you could write the thing out in pseudo code first, and get the algo down cold before you code it and have to debug it umpteen times. That's what I'd be tempted to do. And I'd do it maybe on paper believe me or not.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hint: it's a simple loop in base 36
That's cheating!
;)