Getting run time error

i tried a lot on this error but am not able to solve the problem here is the code

import java.util.*;
public class Main {
public static void main(String args[]) {

Scanner in=new Scanner(System.in);
String p=in.next();
char q;
int r;
for(int i=0;i<p.length();i++)
{
    q=p.charAt(i);
    r=(int)q;
    if(9-r<r)
    {
    p=p.substring(0,i)+r+p.substring(i+1);
    }
    
}
System.out.println(p);
}

}

see the ascii value of an integer is not the same integer…you are assuming that if you will typecast character 0 to integer you will get 0…but actually the ascii value of 0 is somewhere around 47…if you want to get the integer value then write r=q-‘0’;…
for ex)if q=‘3’(ascii value-50)
then r=q(50)-‘0’(47);
so r will have value 3…
one more thing if 9-r is less than r then (9-r) should come in place of ith character(refer to the place where you are taking susbstring)
also you need to take care of some edge cases in your code…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.