 <p><code>replace()</code> will return a new <code>String</code> where ALL occurrences of a particular character are changed, not just a character at a particular position. So your problem is that the repeated <code>replace()</code> statements are effectively modifying the values back and forth.</p> <p>Because a <code>String</code> is immutable, you cannot simply replace its characters with others dynamically. So, convert your code to use a <code>StringBuilder</code> instead.</p> <pre><code>StringBuilder buildSample = new StringBuilder(); buildSample.append(sample); </code></pre> <p>Now you can use <code>setCharAt()</code> instead of <code>replace()</code> to change the character at one position at a time.</p> <pre><code>buildSample.setCharAt(i, 'A'); </code></pre> <p>At the end, you can return <code>buildSample.toString()</code>.</p> <p>As for changing each letter A to F to its complement, if only these six letters are required, a hard-coded function with a <code>switch</code> statement would do. Otherwise you can use a function like <code>complementaryLetter()</code> below, which returns the complement after checking the ASCII value of the character. This will work for all characters. You can add code to handle invalid cases, for non-character input.</p> <p><strong>A complete working code:</strong></p> <pre><code>public class Replace { public static void main(String[] args) { String s1 = "ABCDEFA"; System.out.println(s1); s1 = changeSample(s1); System.out.println(s1); } public static char complementaryLetter(char letter) { char retChar = 'A'; if ((int) letter % 2 == 0) retChar = (char) ((int)letter - 1); else retChar = (char) ((int) letter + 1); return retChar; } public static String changeSample(String sample) { StringBuilder buildSample = new StringBuilder(); buildSample.append(sample); for (int i = 0; i &lt; sample.length(); i++) { buildSample.setCharAt(i, complementaryLetter(sample.charAt(i))); } return buildSample.toString(); } } </code></pre>