Solved

Increment alphanumeric sequence

Posted on 2016-09-07
6
207 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

688 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