Jump to content

Java halp

shypin
Go to solution Solved by Nuluvius,

Your attempt is overcomplicated. Here is a more concise solution... I'm sure you can extend it to work with an array of start and end strings and whatever other guff you need (we won't do all the work for you after all):

import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main{    public static void main(String[] args)    {        for(String s : decode("AAAAAATGAAAAAAAAAAAATGAAATGAAATGAA", "ATG", "TGA"))        {            System.out.println(s);        }    }    private static ArrayList<String> decode(String input, String start, String end)    {        ArrayList<String> result = new ArrayList<String>();        Pattern p = Pattern.compile(Pattern.quote(start) + "(.*?)" + Pattern.quote(end));        Matcher matcher = p.matcher(input);        while (matcher.find())        {            result.add(matcher.group(0));        }        return result;    }}

basically im trying to write a program that finds substrings that matches certain conditions

lets say i have a string of DNA: "AAAAAATGAAAAAAAAAAAATGAAATGAAATGAA"

i would want the program to find substrings that starts with ATG and ends with TGA

so in the above case it should return an array of two strings: "ATGAAAAAAAAAAAATGA" and "ATGAAATGA"

 

below is my code

any help/suggestion would be appreciated

import java.util.ArrayList;public class Decoder {    String string;    String[] result;    public Decoder(String data, String start[], String stop[], int divisibleBy) {        string = data;    }    String[] decode(String data, String[] starts, String[] stops, int divisibleBy) {        System.out.println(data.length());        ArrayList<String> result = new ArrayList<String>();        int index = 0;        while (hasMore(data, starts, stops, divisibleBy) && index < data.length()) {            String subData = data.substring(index, data.length());            String dataFound = findNext(subData, starts, stops, divisibleBy);            result.add(dataFound);            index += data.indexOf(dataFound) + dataFound.length();        }        return result.toArray(new String[result.size()]);    }    private boolean hasMore(String data, String[] starts, String[] stops, int divisibleBy) {        if (findNext(data, starts, stops, divisibleBy) != null) {            return true;        }        return false;    }    private String findNext(String data, String[] starts, String[] stops, int divisibleBy) {        int startIndex = data.indexOf(starts[0]);        System.out.println(startIndex);        for (String start: starts) {            int index = data.indexOf(start);            if (index < startIndex && index != -1) {                startIndex = index;            }        }        System.out.println(startIndex);        if (startIndex == -1) {            System.out.println("not found");            return null;        }        int stopIndex = -1;        for (String stop: stops) {            int index = data.indexOf(stop, startIndex + divisibleBy);            if (index <= stopIndex && index != -1) {                stopIndex = index;            }        }        if ((stopIndex - startIndex) % divisibleBy == 0) {            System.out.println("data returned");            return data.substring(startIndex, stopIndex + divisibleBy);        }        else {            System.out.println("nope");            return null;        }    }}
Link to comment
Share on other sites

Link to post
Share on other sites

below is my code

any help/suggestion would be appreciated

 

You're not telling use what your problem is. You're telling us what the code should do, but you're not telling us what the code actually does. Also it would be helpful if you included the code you're using to test this class. That way we know what valid input should be without having to figure out your code first.

 

 

Also consider fixing your class.

public class Decoder {    String string;    String[] result;    public Decoder(String data, String start[], String stop[], int divisibleBy) {        string = data;    }    //...}

In this code you're passing multiple things into the constructor, but only saving data. On top of that, data isn't even being used in your class methods. Nor is result which you've also defined for your class. All the methods in Decoder require the same arguments you already got in the constructor which is unnecessary if you saved all that data in your class. What you're doing looks like it should be a bunch of static methods instead of instance methods. You should choose one or the other.

Link to comment
Share on other sites

Link to post
Share on other sites

Your attempt is overcomplicated. Here is a more concise solution... I'm sure you can extend it to work with an array of start and end strings and whatever other guff you need (we won't do all the work for you after all):

import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main{    public static void main(String[] args)    {        for(String s : decode("AAAAAATGAAAAAAAAAAAATGAAATGAAATGAA", "ATG", "TGA"))        {            System.out.println(s);        }    }    private static ArrayList<String> decode(String input, String start, String end)    {        ArrayList<String> result = new ArrayList<String>();        Pattern p = Pattern.compile(Pattern.quote(start) + "(.*?)" + Pattern.quote(end));        Matcher matcher = p.matcher(input);        while (matcher.find())        {            result.add(matcher.group(0));        }        return result;    }}

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Your attempt is overcomplicated. Here is a more concise solution... I'm sure you can extend it to work with an array of start and end strings and whatever other guff you need (we won't do all the work for you afteral):

Thanks! That is exactly what i needed. Didnt think about using regex xD

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×