-5

this is my first time in this website, I'm not english so srry for my broken english, I started programming one or to months ago so srry if I say some dumb things. I'm trying to create a program in which you have to insert strings made by the letters AGTC, it will stop asking for strings when I insert a point. This program changes this letters (A for T, T for A, G for C and C for G), when the letters are changed it prints all the strings inverted, here i give an example:

input: ATGCATGC GTCGTGA . Output: GCATGCAT (inverted) TCACGAC (inverted)

The problem is in the last loop in whcih I want to print the string inverted. Pd: I'm trying to give the most info I can to make it easy for you it's mY first time, plz understand.

    Scanner t = new Scanner(System.in);
    String [] cad = new String [20]; //Strings which we are going to enter (made by AGTC..);
    String [] fin = new String [20]; //Strings which I want to print 
    int [] length = new int [20]; //I took the lenght of ever string to change the letters from each one.
    boolean [] ver = new boolean [20]; //This boolean is to stop the loop, when I type a point the loop must stop.
    System.out.println("Put ADN chains (Finish with a point)");
    int count = 0;
    for (int i=0; i<20; i++) {
        cad[i]=t.nextLine();
        cad[i]=cad[i].toUpperCase();
        length [i] = cad[i].length();
        fin[i]=cad[i].replaceAll("T","a");
        fin[i]=fin[i].replaceAll("A","t");
        fin[i]=fin[i].replaceAll("G","c");
        fin[i]=fin[i].replaceAll("C","g");

        ver [i] = cad[i].equals("."); //The way the loop should stop.
        if (ver[i]==true){
            break;
        }
        if (ver[i]==false){
            count++; //I made this counter only to know how many strings they have inserted.
        }
    }
    for (int i=0;i<count; i++) {
            for (int j=0;j<length[i]; i--) { //Here is the main problem, I got the chain with the letters changed but I need to invert the chain.
            System.out.println(fin[i].toUpperCase());     
        }
    }
  • Can you please try to explain more ? try to give more examples or explain the one provided, because you said A for T but the output A is changed with G – Elarbi Mohamed Aymen Apr 06 '18 at 16:08
  • Does it even have to be Java? Looks like `cat | tr 'ATGC' 'TACG'` would be enough. Guess how many hours it took me to write... Otherwise, nobody can help you with your program if you don't show some code and explain precisely what the problem is. Google 'mcve'. – Andrey Tyukin Apr 06 '18 at 16:54
  • Relevant: https://stackoverflow.com/questions/7457886/is-there-a-way-to-use-tr-or-equivalent-in-java There is a `StringUtils.replaceChars` provided by ApacheCommons. – Andrey Tyukin Apr 06 '18 at 17:07

1 Answers1

1

There is a method for Strings, called replaceAll, which you can use to replace parts of the String, like:

  "ATGCATGC GTCGTGA .".replaceAll ("A", "T");

In the upcomming java9, there is a jshell, ideally suited to test such things.

-> "ATGCATGC GTCGTGA .".replaceAll ("A", "T")
|  Expression value is: "TTGCTTGC GTCGTGT ."
|    assigned to temporary variable $85 of type String

So on the first look, it seems, as if calling 4 such methods in a chain will give the result, but:

-> "ATGCATGC GTCGTGA .".replaceAll ("A", "T").replaceAll ("T", "A").replaceAll ("G", "C").replaceAll ("C", "G")
|  Expression value is: "AAGGAAGG GAGGAGA ."
|    assigned to temporary variable $1 of type String

the transformed As are retransformed back from the next transformation, but we can use a trick - transform to lowercase:

-> "ATGCATGC GTCGTGA .".replaceAll ("A", "t").replaceAll ("T", "a").replaceAll ("G", "c").replaceAll ("C", "g")
|  Expression value is: "tacgtacg cagcact ."
|    assigned to temporary variable $2 of type String

and in the end, make a call to uppercase everything:

-> String dna = "ATGCATGC GTCGTGA .".replaceAll ("A", "t").replaceAll ("T", "a").replaceAll ("G", "c").replaceAll ("C", "g").toUpperCase ()
|  Modified variable dna of type String with initial value "TACGTACG CAGCACT ."
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • Check this out: https://stackoverflow.com/questions/7457886/is-there-a-way-to-use-tr-or-equivalent-in-java I couldn't come up with a better solution using only standard library, though. – Andrey Tyukin Apr 06 '18 at 17:08