0

Hi I am trying to do some practice in java, I found some difficulties on it. My code:

import java.util.regex.; import java.util.;

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

        System.out.println("Please input a Sequence of DNA: ");
        String dnaInput=in.nextLine();

        if (dnaInput.matches("[ATCG]+$")) {
            dnaSequence=dnaInput;
            int [] count=new int[4];
            if (dnaSequence!=null) {
                for (int i=0; i<dnaSequence.length(); i++) {
                    switch(dnaSequence.charAt(i)) {
                        case 'A':
                            count[0]++;
                            break;
                        case 'T':
                            count[1]++;
                            break;
                        case 'C':
                            count[2]++;
                            break;
                        case 'G':
                            count[3]++;
                            break;
                        default:
                            System.out.println("Sorry You have invalid Type DNA sequence");
                    }
                }
                System.out.println("Now : A"+count[0]);
                System.out.println("Now : T"+count[1]);
                System.out.println("Now : C"+count[2]);
                System.out.println("Now : G"+count[3]);

                /*
                Pattern  p=Pattern.compile("A");
                Matcher m=p.matcher(dnaSequence);
                int j=0;
                while(m.find())
                j++;
                System.out.println(j);
                */
            }
        } else {
            System.out.println("THE SEQUENCE SHOULD BE CONTAIN ATCG");
        }
    }
}

Let's say my input AAATATTTTGGGCC , Now how i can compressed this to A3TAT4G3C2 , what is the process i should follow And also how Ii can decompress it again. like how i can get this AAATATTTTGGGCC again

6
  • You can drop the null check: dnaSequence will never be null. Commented Oct 14, 2014 at 2:57
  • 1
    What are you having trouble with? Note that your code as-shown doesn't perform either process you've described. Commented Oct 14, 2014 at 2:59
  • 1
    You might like to do some research into "Run length encoding"... Commented Oct 14, 2014 at 3:01
  • 1
    If you ask a specific question, we'll be happy to answer. However, this site is not meant for quick solutions to homework problems. Your question seems like a broad outline of a homework assignment followed by skeleton code that does nothing. Commented Oct 14, 2014 at 3:01
  • Here's a hint: loop over each character in the string and keep track of its value. Then you're only interested in counting occurrences of matching characters. Once you encounter a new character (that doesn't match), stop counting, update the output, and start looking for matches again. Commented Oct 14, 2014 at 3:03

2 Answers 2

1

You implement decompress your own.

import java.util.Scanner;

public class CompressDecompress {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("Please input a Sequence of DNA: ");
        String dnaInput=in.nextLine();
        System.out.println("Enter E for encrypt or Enter D for decrypt");
        String function=in.nextLine();
        if("E".equalsIgnoreCase(function)){
            System.out.println(compress(dnaInput) );
        }else if("D".equalsIgnoreCase(function)){
            System.out.println(deCompress(dnaInput) );
        }else{
            System.out.println("Invalid Input");
        }
    }

    private static String compress(String toCompress){
        StringBuilder compressedSB = new StringBuilder();

        char tempChar=' ';
        for (int i=0,counter=1;i<toCompress.length();i++){
            if(tempChar!=toCompress.charAt(i)){
                tempChar=toCompress.charAt(i);
                compressedSB.append(tempChar);
                if(counter>1){
                    compressedSB.append(counter);
                    counter=1;
                }
            }else{
                counter++;
            }
            if (i==toCompress.length()-1){
                compressedSB.append(counter);
            }
        }
        return compressedSB.toString();
    }

    private static String deCompress(String toDeCompress){
        return null;    
    }
}

And of course add the validation using regex or CASE for 'ATCG'

Sign up to request clarification or add additional context in comments.

Comments

0

Sharing my implementation if you want to do it without using string regex:

public String compress(String str) {
 String mystr = "";
 char last = str.charAt(8);
 int count = 1;
 for (int i = 1; i < str.lengthQ; i++) {
 if (str.charAt(i) == last) { // Found repeat char
 count++;
 } else { // Insert char count, and update last char
 mystr += last + "" + count;
 last = str.charAt(i);
 count = 1;
}
 }

return mystr + last + count;
}

FYI I too implemented only compress feature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.