I am writing a Java program to encrypt a four digit number. Some procedures require to swap 1st digit with 3rd and 2nd with 4th... Here's what I have written, but it's not working. for example, if my input is 9876 then after these four swaps it gives output 9898. which means that first and third Swaps are not working. But if I comment the second and fourth swaps then 1st and 3rd swap works.
char ch;
ch=str.charAt(0);
str=str.replace(str.charAt(0),str.charAt(2));
str=str.replace(str.charAt(2),ch);
ch=str.charAt(1);
str=str.replace(str.charAt(1),str.charAt(3));
str=str.replace(str.charAt(3),ch);