Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Increment alphanumeric sequence

Posted on 2016-09-07
6
Medium Priority
?
413 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-
  • 4
6 Comments
 
LVL 17

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 17

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
krakatoa earned 2000 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 17

Expert Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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