2021年1月19日星期二

.replace() method in Java replaces what is replaced

Below is my code. I am trying to input "ATC" to get an output of "UAG". All it does is replace 'A' with 'U', 'T' with 'A', 'C' with 'G', and 'G' with 'C' (Just like transcription - DNA to mRNA).

The problem is... when I input ATC, AGC, TGC, or anything with 'C' at the end, the program will replace 'C' with 'G' and then proceed to replace the new 'G' with a 'C'.

.replace('C','G').replace('G','C');

How can I stop the program from replacing the 'C' back to a 'C'? My input should be AGC, and the intended output should be UCG.

import java.util.*;    public class Main {        public static void main(String[] args) {          Scanner scanner = new Scanner(System.in);            String codon = scanner.next();                String basePairing = codon                      .replace('A','U').replace('T','A')                      .replace('C','G').replace('G','C');              System.out.println(basePairing);        }  }  
https://stackoverflow.com/questions/65783561/replace-method-in-java-replaces-what-is-replaced January 19, 2021 at 07:37AM

没有评论:

发表评论