public static String encryptLine(char[] cipher, String line) { char[] alphabet ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p', 'q','r','s','t','u','v','w','x','y','z'}; for (int j = 0; j < 26; j++) { char b = alphabet[j]; char c = cipher[j]; String bString = Character.toString(b); String cString = Character.toString(c); line = line.replace(bString, cString); } return line; }
String line is later outputted by a PrintStream. The portion that is wrong with my program is shown above. I am trying to encrypt lines of String in a text file. The above code doesn't work because once you replace a character from cipher[], it can later be changed again during the loop. Any ideas on how I should go about doing this?