Yoni Yoni - 1 month ago 11
Java Question

Java: Count ATC from a DNA sequence

I want to count the A, T and C from a DNA input in a textarea.
I got this so far

String Seq = jTextArea1.getText();

int aCounter = 0;
for( int i=0; i<Seq.length(); i++ ) {
if(Seq.charAt(i) == 'A' ) {
aCounter++;
System.out.println(aCounter);


Now I want to count the T and Cs too. I tried to copy this code for the T and C but it was not working.

Answer
    String Seq = jTextArea1.getText();

    int aCounter = 0;
    int tCounter = 0;
    int cCounter = 0;
    for( int i=0; i<Seq.length(); i++ ) {
        if(Seq.charAt(i) == 'A' ) {
           aCounter++;}
        elseif(Seq.charAt(i) == 'T' ) {
           tCounter++;}
        elseif(Seq.charAt(i) == 'C' ) {
           cCounter++;}
    }
    System.out.println(aCounter);
    System.out.println(tCounter);
    System.out.println(cCounter);
Comments