So i have this string StringBuilder fenToString = new StringBuilder("1P111Pr1")
now how can i change it to a = "1P3Pr1"?
i tried this
int fenNum = 0;
for(int i = 0; i < fenToString.length(); i++){
        if(Character.isDigit(fenToString.charAt(i))){
            fenNum += 1;
            fenToString.setCharAt(i, (char)(fenNum+'0'));
        }else{
            fenNum = 0;
        }
    }
i get "1P123Pr1" instead of "1P3Pr1"
 
                        
So to explicitly state the task: You want to convert a sequence of two or more digits into the sum of those digits. The sum is guaranteed to be 8 or less, i.e. to also be a single digit.
There are many ways to do that, but the closest to what you're trying would likely be:
Tests
Output