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 using Map?

Hi,

Dear folks,

Here is what i want to do and thankfully was able to get answer using thread below with List
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000BjjoQAC
,but now i am rewriting the same code using Map,but am getting error as 
 
Method does not exist or incorrect signature: void get(String) from the type Map
Here is what the end goal is 
 
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

Here is code with Map
public class SDASMapConvert {
    

    
    
public static Integer returnInteger(String z, integer k){
        Integer retInt;
        String key = 'abcdefghijklmnopqrstuvwxyz';

        List<String>myAlphabets = z.split('');
        Map<String, String> myMap = new Map<String, String>();
        String strarr = '';

       for (integer i = 0; i < myAlphabets.size(); i += 2) {
        myMap.put(myAlphabets[i], myAlphabets[i + 1]);
    }

    for (String s : myMap.keySet()) {
        System.debug(s + ' is ' + map.get(s));
        strarr += string.valueOf(s);
    }
        
        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;
    }
    


}

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

Are you looking something like below:
 
Public class SDASMapConvert {
    
    public static Integer returnInteger(String z, integer k){
        Integer retInt;
        String key = 'abcdefghijklmnopqrstuvwxyz';
        
        List<String>myAlphabets = z.split('');
        Map<String, String> myMap = new Map<String, String>();
        String strarr = '';
        
        for (integer i = 0; i < myAlphabets.size(); i++) {
            String intNum = String.valueOf(key.indexOf(myAlphabets[i].toLowerCase())+1);
            myMap.put(myAlphabets[i], intNum);
        }
        
        for (String s : myMap.keySet()) {
            System.debug(s + ' is ' + myMap.get(s));
            strarr += string.valueOf(myMap.get(s));
        }
        
        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;
    }    
    
}

Thanks,
Maharajan.C

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Fiona,

Could be more specific like for which line you are getting that error.

Thanks!
Maharajan CMaharajan C
Hi Flona,

Are you looking something like below:
 
Public class SDASMapConvert {
    
    public static Integer returnInteger(String z, integer k){
        Integer retInt;
        String key = 'abcdefghijklmnopqrstuvwxyz';
        
        List<String>myAlphabets = z.split('');
        Map<String, String> myMap = new Map<String, String>();
        String strarr = '';
        
        for (integer i = 0; i < myAlphabets.size(); i++) {
            String intNum = String.valueOf(key.indexOf(myAlphabets[i].toLowerCase())+1);
            myMap.put(myAlphabets[i], intNum);
        }
        
        for (String s : myMap.keySet()) {
            System.debug(s + ' is ' + myMap.get(s));
            strarr += string.valueOf(myMap.get(s));
        }
        
        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;
    }    
    
}

Thanks,
Maharajan.C
This was selected as the best answer
State InfosState Infos
Great info.Thanx for sharing. You can Visit my Page State Infos (https://stateinfos.com/) for more Valuable information. Govt of India recently launched a new app named PM Daksh App (https://stateinfos.com/pm-daksh-portal-online-registration/). Also, you can check out Duare Sarkar camp List (https://stateinfos.com/duare-sarkar-camp/)
uaye ertyuaye erty
This was the most important information for me because I am working on man van (https://proremovalsleeds.co.uk/man-van-leeds/) project and needed this information which was creating problems for me.
Chinky Sharma 9Chinky Sharma 9