Solved

Increment alphanumeric sequence

Posted on 2016-09-07
6
164 Views
Last Modified: 2016-09-17
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.
0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 41788617
Homework?
0
 

Author Comment

by:-Dman100-
ID: 41788694
Something like that...any ideas on how to accomplish an incrementing sequence like that?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41788828
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.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 41792068
I am away for a few days, but as you seem to be stuck, I am posting this piece of starter code (completely different from the piece you are working on), which would need to be extended to do the full job. See what you can do with it if you are interested in this approach.

class Rotor{


static String endString = "ZZZ";
static String currentString = "000";
static char[] cChA = currentString.toCharArray();
static int ipointer = endString.length()-1;
static int apointer = 0;

  public static void main (String [] args){

	while(!(currentString.equals(endString))){
	
		if(Character.isDigit(cChA[ipointer])&&(int)cChA[ipointer]<57){ // letter and it is less than 9.
			cChA[ipointer]=(char)(cChA[ipointer]+1);
			currentString = String.valueOf(cChA);
			System.out.println(currentString);
				try{
				Thread.sleep(25);
				}catch(Exception e){}
		}
		else if(Character.isDigit(cChA[ipointer])){ // letter and it is 9.
			cChA[ipointer]='0'; //set it to 0. 
			
			for(int c=ipointer-1;c>-1;c--){ //increment upstream rotors by 1, if it is < 9 and then break out. 
				if((int)cChA[c]<57){
					cChA[c]=(char)(cChA[c]+1);
					currentString = String.valueOf(cChA);
					System.out.println(currentString);
						try{
						Thread.sleep(25);
						}catch(Exception e){}
					break;
				}
				else{cChA[c]='0';} // Otherwise current rotor == 9 so set it to 0.
				
			}
		} //At this point, all the numerics are done and have been printed. (Although the loop will continue to run *). Continue below here to further the algorithm into alphanumeric and alpha-only.
		
		if(true){} // reserved for future use.
		
		if(currentString.equals("999")){System.exit(0);} // * Remove this line when you are ready to code further.
	}

  }

}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41801596
Hint: it's a simple loop in base 36
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41802692
That's cheating!
;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question