Solved

Increment alphanumeric sequence

Posted on 2016-09-07
6
116 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 46
Custom Java Application connects to database: Which one of the 20? 4 27
Question! 4 36
SqlDataBase 7 46
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

816 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

10 Experts available now in Live!

Get 1:1 Help Now