Solved

Increment alphanumeric sequence

Posted on 2016-09-07
6
78 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 16

Expert Comment

by:krakatoa
Comment Utility
Homework?
0
 

Author Comment

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

Expert Comment

by:krakatoa
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
Comment Utility
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
Comment Utility
Hint: it's a simple loop in base 36
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
That's cheating!
;)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now