Check whether
str
contains value 3. If contains, replace 3 with x
if (str.contains("3")) {
str = str.replace("3", "x");
}
To make all multiples of 3 replace by x , use below condition
if (num % 3 == 0) {
str = str.replace(String.valueOf(num), "x");
}