+ Start a Discussion
Leonard CadetLeonard Cadet 

Nested For Loop Issue

I am trying to create a loop that creates pallet locations and puts that information into a record. 

The first loop goes through the Section which is 1 to 46
The nested loop goes through the Aisle which is A to Q

I expect to start with the first section and then complete all the aisle before going back to iterating the through the Section again 

So when the loop starts I expected it to start the section at 1 and loop through the Aisle like so:
A1A
B1A
C1A
etc
then once it hits q it loops out, increases the section by 1 and does the nested loop again
A2A
B2A
C2A 
Can I get feedback on my code? 
List<Stock_Location__c > slnewt = new List <Stock_Location__c>();
StringBuilder sb = new StringBuilder();

for (Integer i = 1; j = 0; i < 46; i++) {

    for(char alphabet = 'a'; alphabet <= 'q'; alphabet++) {
    Stock_Location__c slnewt = new Stock_Location__c(
    Aisle__c = sb.append(alphabet),
    Section__c = i ,
    Shelf__c = 'a',
    barcode__c = sb.append(alphabet) + i + 'a',
    Location_Name__c =   sb.append(alphabet) + '-'  i+ '-'+ 'a'   
    );
    }

    slnewt.add(slnew);
}

Insert slnewt;

 
Best Answer chosen by Leonard Cadet
Jitendra Singh ShahiJitendra Singh Shahi

Hi,

you can use below code to generate your sequence A1A, B1A... etc

 

integer j=0
String[] charArr = new String[]{'A','B','C','D'};
for(integer i=1;i<46;i++){
    j=i;
    String atlast='';
    for(integer k = 0; k < charArr.size(); k++) {
         if(j==i){
              atlast = charArr[k];
              j++;
         }
         system.debug(charArr[k]+i+atlast);   
    }
}
 

it will debug this 

 

User-added image

I hope this will help you. Please mark it best answer if it helps you.

All Answers

Jitendra Singh ShahiJitendra Singh Shahi

Hi,

you can use below code to generate your sequence A1A, B1A... etc

 

integer j=0
String[] charArr = new String[]{'A','B','C','D'};
for(integer i=1;i<46;i++){
    j=i;
    String atlast='';
    for(integer k = 0; k < charArr.size(); k++) {
         if(j==i){
              atlast = charArr[k];
              j++;
         }
         system.debug(charArr[k]+i+atlast);   
    }
}
 

it will debug this 

 

User-added image

I hope this will help you. Please mark it best answer if it helps you.

This was selected as the best answer
Leonard CadetLeonard Cadet
This foundation worked for me, I made some small changes but the ultimate idea of using the array was the biggest time saver below is the code I ended up running and working with. I manually changed the atlast variable from A to B to C to D
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reference Code for completing task 
//https://developer.salesforce.com/forums/ForumsMain?id=9062I000000QyUy
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

List<Stock_Location__c > slnewt = new List <Stock_Location__c>();
//integer j=0;
String[] charArr = new String[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'};
for(integer i=12;i<47;i++){
    //j=i;
    String atlast='';
    
    for(integer k = 0; k < charArr.size(); k++) {
         string s1=string.valueof(i);
        // string s4 = string.valueof(atlast);
        // string s2 = string.valueof(charArr[k]);
       //  if(j==i){
              atlast = 'B';
              //j++;            
              Stock_Location__c slnew = new Stock_Location__c(
              Aisle__c = charArr[k],
              Section__c = s1 ,
              Shelf__c = atlast,
              barcode__c = charArr[k]+s1+atlast,
              Name= charArr[k]+'-'+s1+'-'+atlast              
            );slnewt.add(slnew);
        // }  
           system.debug(charArr[k]+s1+atlast);
           system.debug(charArr[k]+'-'+s1+'-'+atlast);    
    }
}

Insert slnewt;

r. Thank you!