function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

How To convert string into an integer by replacing each letter with its position in the alphabet

Dear folks,

Here is what i want to do
 
For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following operations:

Convert: "zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
Transform #2: 17 ➝ 1 + 7 ➝ 8



Hence ,Wrote a apex class for To  convert string into an integer by replacing each letter with its position in the alphabet,but it doesnt seems working,error i get on Anonymus Console Window for below command
is 
Line: 14, Column: 1
System.TypeException: Invalid integer: a
Here is anonymous code
SDSAfterConvert.returnInteger();

Here is apex code
 
public class SDSAfterConvert {
    
    
public static List<Integer> returnInteger(){


List<Integer> inputList = new List<Integer>();
             
String[] myAlphabets = new String[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g'};
 
for (String s1 : myAlphabets) {
   
    System.debug(s1);
    Integer d = integer.ValueOf(s1);
    inputList.add(d);
}
        
return inputList;
             
}
    
}



Regards,
Fiona​​​​​​​
 
Best Answer chosen by fiona gentry
Maharajan CMaharajan C
Hi flona,

Please try the below code:

Pass the String("zbax")  and  Transform #( 2 ) as input then you will get the final o/p as 8.
 
public class SDSAfterConvert {
    public static Integer returnInteger(String z, integer k){
        Integer retInt;
        String key = 'abcdefghijklmnopqrstuvwxyz';
        String[] myAlphabets = z.split('');
        String strarr = '';
        for (String s1 : myAlphabets) {
            Integer i = key.indexOf(s1.toLowerCase())+1;
            System.debug(s1 + ' --> '  +  i );
            strarr += string.valueOf(i);
        }
        
        system.debug('strarr --> ' + strarr);
        String s = '';
        for(integer i = 0; i < k ; i++){
            system.debug(' strarr --> ' + strarr);
            Integer total = 0;
            for(String s1 : strarr.split('')){
                total += integer.valueOf(s1);
            }
            system.debug(' total --> ' + total);
            strarr = string.valueOf(total);
        }
        
        system.debug(' ************* ' + strarr);
        if(!string.isEmpty(strarr))
            retInt = integer.valueOf(strarr);
        
        system.debug(' ************* ' + retInt);
        
        return retInt;
    }
}

To execute the code from console use the below line:
 
SDSAfterConvert.returnInteger('zbax' , 2);

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi flona,

Please try the below code:

Pass the String("zbax")  and  Transform #( 2 ) as input then you will get the final o/p as 8.
 
public class SDSAfterConvert {
    public static Integer returnInteger(String z, integer k){
        Integer retInt;
        String key = 'abcdefghijklmnopqrstuvwxyz';
        String[] myAlphabets = z.split('');
        String strarr = '';
        for (String s1 : myAlphabets) {
            Integer i = key.indexOf(s1.toLowerCase())+1;
            System.debug(s1 + ' --> '  +  i );
            strarr += string.valueOf(i);
        }
        
        system.debug('strarr --> ' + strarr);
        String s = '';
        for(integer i = 0; i < k ; i++){
            system.debug(' strarr --> ' + strarr);
            Integer total = 0;
            for(String s1 : strarr.split('')){
                total += integer.valueOf(s1);
            }
            system.debug(' total --> ' + total);
            strarr = string.valueOf(total);
        }
        
        system.debug(' ************* ' + strarr);
        if(!string.isEmpty(strarr))
            retInt = integer.valueOf(strarr);
        
        system.debug(' ************* ' + retInt);
        
        return retInt;
    }
}

To execute the code from console use the below line:
 
SDSAfterConvert.returnInteger('zbax' , 2);

Thanks,
Maharajan.C
This was selected as the best answer
jhkguhf fgtrergfdjhkguhf fgtrergfd
There are some simple ways to convert string into an integer you can visit here (https://dewsburyremovals.co.uk/student-removals-dewsbury/) to learn about it. Specially if you want to change each letter with its position.