-Dman100-
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:
I was trying to modify the following method to make it work, but am not having much luck
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.
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)
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;
}
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.
Homework?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hint: it's a simple loop in base 36
That's cheating!
;)
;)