how can I create a loop that also turns string "abcc" into the sum of their letter position, say a=1 b=2 c=3 and it sums the string 1+2+3+3=9.
    import java.util.Arrays;
    public class Test
    {
            public static void main(String[] args)
            {
            String original = "hello";
            char[] chars = original.toCharArray();
            Arrays.sort(chars);
            String sorted = new String(chars);
            System.out.println(sorted);
                }
           }
                You can use the ASCII values. a has value 97, b has 98 and so on.
private int printSum(String original){
    int sum = 0;
    if(original!=null){
        char[] arr = original.toLowerCase().toCharArray();
        for(int x :arr){
            sum+= (x-96);
        }
    }
    return sum;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With