IsaDavRod
IsaDavRod

Reputation: 39

.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);

    }
}

Upvotes: 0

Views: 125

Answers (3)

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

Here is a little characterSwap() method you may find useful:

public static String characterSwap(String inputString, Character... characters) {
    if (characters.length == 0 || characters.length % 2 != 0) {
        throw new IllegalArgumentException("characterSwap() Method Error! "
                    + "Invalid parameter pairing for characters swapping!");
    }
    StringBuilder sb = new StringBuilder("");
    for (int i = 0; i < inputString.length(); i++) {
        Character curChar = inputString.charAt(i);
        for (int j = 0; j < characters.length; j += 2) {
            if (characters[j] != null && curChar.equals(characters[j])) {
                curChar = characters[j + 1];
                break;
            }
        }
        if (curChar != null && curChar != 0) {
            sb.append(curChar.toString());
        }
    }
    return sb.toString();
}

To use it:

String str = characterSwap("ATC, AGC, TGC", 'A', 'U', 'T', 'A', 'C', 'G', 'G', 'C');
System.out.println(str);

Any 'A' encountered is replaced with 'U', any 'T' encountered is replaced with 'A', any 'C' encountered is replaced with 'G', and any 'G' encountered is replaced with 'C'.

The console output will be:

UAG, UCG, ACG

The swap characters can be provided as a Character[] array as well. If you want to remove a specific character then pass null or '\0' in your character swapping pairs, for example:

String str = characterSwap("ATC, AGC, TGC", 'A', 'U', 'T', null, 'C', 'G', 'G', 'C');

Here, when a 'T' is encountered, it is removed.

The console output will be:

UG, UCG, CG

Upvotes: 1

aran
aran

Reputation: 11830

String codon = scanner.next();             /*ATGC*/
char[] codonArr = codon.toCharArray();
for (int i=0;i<codonArr.length;i++)
{
     switch(codonArr[i])
     {              
        case 'A': {codonArr[i]='U';break;}
        case 'T': {codonArr[i]='A';break;}
        case 'C': {codonArr[i]='G';break;}
        case 'G': {codonArr[i]='C';break;}
     }
}
        
codon = new String(codonArr);
System.out.println(codon);                 /*UACG*/

In : ATGC

Out: UACG

Upvotes: 1

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79035

You can do it by conditional branching and by using String#replaceFirst as shown below:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String codon = scanner.next();

        String basePairing = codon.contains("G")?
                                codon.replace('A','U').replace('T','A')
                                .replace('C','G').replaceFirst("G", "C"):
                                        codon.replace('A','U').replace('T','A')
                                        .replace('C','G');
        System.out.println(basePairing);
    }
}

A sample run:

AGC
UCG

Another sample run:

ATC
UAG

Upvotes: 1

Related Questions