为什么for循环最终以DNA [2]最终出现。

发布于 2025-02-06 12:25:38 字数 1274 浏览 1 评论 0原文

我一直在研究Java编程,以在DNA链中找到该基因。我的问题是,为什么FO循环最终在DNA [2]处最终,并且输出卡在第三个字符串上,并且不再返回结果。以下是代码:

import edu.duke.*;
import java.io.*;


public class Part1 {
        public String findSimpleGene (String dna) {
            String result = "";
            int startIndex = dna.indexOf("atg");
            int stopIndex = dna.indexOf("taa", startIndex+3);
            result = dna.substring(startIndex, stopIndex+3);
            if (startIndex != -1 
            && stopIndex != -1 
            && (stopIndex - startIndex) % 3 ==0) {
                return result;
            }
            else {
                return "Not Exist";
            }
            
        }
        public void testSimpleGene () {
            String dna[] = new String[5];
            dna[0] = "cccatggggtaaaaatgataataggagagagagagagagttt";
            dna[1] = "ccatggggtctaaataataa";
            dna[2] = "atggggcgtaaagaataa";
            dna[3] = "acggggtttgaagaatgaaccaat";
            dna[4] = "acggggtttgaagaatgaaccaataacga";
            for (int i=0; i < 5; i++) {
            String result = findSimpleGene(dna[i]);
            System.out.println("DNA strand:" + dna[i]);
            System.out.println("Gene:" + result);
        }
        }
}

谢谢!

I've been working on java programming developed to find the gene in DNA strands. My question is why the for loop ends up at dna[2] and the output is stuck at the third String and no more results are returned. The following is the code:

import edu.duke.*;
import java.io.*;


public class Part1 {
        public String findSimpleGene (String dna) {
            String result = "";
            int startIndex = dna.indexOf("atg");
            int stopIndex = dna.indexOf("taa", startIndex+3);
            result = dna.substring(startIndex, stopIndex+3);
            if (startIndex != -1 
            && stopIndex != -1 
            && (stopIndex - startIndex) % 3 ==0) {
                return result;
            }
            else {
                return "Not Exist";
            }
            
        }
        public void testSimpleGene () {
            String dna[] = new String[5];
            dna[0] = "cccatggggtaaaaatgataataggagagagagagagagttt";
            dna[1] = "ccatggggtctaaataataa";
            dna[2] = "atggggcgtaaagaataa";
            dna[3] = "acggggtttgaagaatgaaccaat";
            dna[4] = "acggggtttgaagaatgaaccaataacga";
            for (int i=0; i < 5; i++) {
            String result = findSimpleGene(dna[i]);
            System.out.println("DNA strand:" + dna[i]);
            System.out.println("Gene:" + result);
        }
        }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随梦而飞# 2025-02-13 12:25:39

对于DNA [3] =“ acgggggggtttttttgaagaatgaaccaat”
startIndex是14,potindex为-1 ,因为没有“ taa”
之后,您发现 substring(14,2)在这种情况下将抛出异常。
方法投掷'java.lang.stringIndexoutofBoundSexception'例外

For dna[3] = "acggggtttgaagaatgaaccaat"
startIndex is 14 and stopIndex is -1 since there is no "taa"
After that you are finding subString(14,2) which will throw below exception in this case.
Method threw 'java.lang.StringIndexOutOfBoundsException' exception

断桥再见 2025-02-13 12:25:39

对于

dna[3] = "acggggtttgaagaatgaaccaat";

您的代码,请扔StringIndexoutofBoundSexception exception&amp;因此,您认为它不再前进到下一个循环的迭代:

int startIndex = dna.indexOf("atg"); // gives 14
int stopIndex = dna.indexOf("taa", startIndex + 3); // gives -1
result = dna.substring(startIndex, stopIndex + 3); //throws `StringIndexOutOfBoundsException` because startIndex is larger than stopIndex.

为了避免这种情况,您可以在调用substring之前检查startIndex始终小于oferindex:

if(startIndex < stopIndex && startIndex!=-1 &&  stopIndex < dna.length()){
 result = dna.substring(startIndex, stopIndex + 3);
 ..}

For

dna[3] = "acggggtttgaagaatgaaccaat";

Your code is throwing StringIndexOutOfBoundsException exception & hence you sees it is no longer advancing to next iteration of for loop :

int startIndex = dna.indexOf("atg"); // gives 14
int stopIndex = dna.indexOf("taa", startIndex + 3); // gives -1
result = dna.substring(startIndex, stopIndex + 3); //throws `StringIndexOutOfBoundsException` because startIndex is larger than stopIndex.

To avoid this kind of scenario, you can check startIndex is always less than stopIndex before calling substring:

if(startIndex < stopIndex && startIndex!=-1 &&  stopIndex < dna.length()){
 result = dna.substring(startIndex, stopIndex + 3);
 ..}
合约呢 2025-02-13 12:25:39

问题在于您编写findsimplegene方法的方式,特别是当您尝试检索stopindex时。实际上,在此上下文中3)您可能超出给定字符串的长度。另外,计算stopIndex startIndex可能是-1毫无意义。

您可以在计算之前添加支票,例如三元运算符,该操作员控制startIndex不是-1,并且会计3仍然使其在给定的String中。附带说明,仅仅为了减少代码,您就可以将结果设置为“不存在”,并且仅在可以进行子字符串(较少IF-ELSE分支和更好的可读性)时才将其更新。

public String findSimpleGene(String dna) {
    String result = "Not Exist";
    int startIndex = dna.indexOf("atg");
    int stopIndex = startIndex >= 0 && startIndex + 3 < dna.length() ? dna.indexOf("taa", startIndex + 3) : -1;

    if (startIndex != -1 && stopIndex != -1 && (stopIndex - startIndex) % 3 == 0) {
        result = dna.substring(startIndex, stopIndex + 3);
    }

    return result;
}

输出

DNA strand:cccatggggtaaaaatgataataggagagagagagagagttt
Gene:atggggtaa
DNA strand:ccatggggtctaaataataa
Gene:Not Exist
DNA strand:atggggcgtaaagaataa
Gene:Not Exist
DNA strand:acggggtttgaagaatgaaccaat
Gene:Not Exist
DNA strand:acggggtttgaagaatgaaccaataacga
Gene:atgaaccaataa

也是测试上述代码的链接:

https://www.jdoodle.com/ iembed/v0/s7f

The problem lies in the way you've written your findSimpleGene method, specifically when you try to retrieve stopIndex. In fact, in that context startIndex could be at the end of the string or less than 3 characters before the end, so when you're performing dna.indexOf("taa", startIndex + 3) you might be exceeding the length of the given String. Also, computing stopIndex when startIndex could be -1 makes little sense.

You could add a check before the computation, like a ternary operator, which controls that startIndex is not -1 and accounting 3 to it still makes it within the given String. On a side note, just to reduce your code, you could already set result to "Not Exist" and update it only if the substring can take place (less if-else branching and better readability).

public String findSimpleGene(String dna) {
    String result = "Not Exist";
    int startIndex = dna.indexOf("atg");
    int stopIndex = startIndex >= 0 && startIndex + 3 < dna.length() ? dna.indexOf("taa", startIndex + 3) : -1;

    if (startIndex != -1 && stopIndex != -1 && (stopIndex - startIndex) % 3 == 0) {
        result = dna.substring(startIndex, stopIndex + 3);
    }

    return result;
}

Output

DNA strand:cccatggggtaaaaatgataataggagagagagagagagttt
Gene:atggggtaa
DNA strand:ccatggggtctaaataataa
Gene:Not Exist
DNA strand:atggggcgtaaagaataa
Gene:Not Exist
DNA strand:acggggtttgaagaatgaaccaat
Gene:Not Exist
DNA strand:acggggtttgaagaatgaaccaataacga
Gene:atgaaccaataa

Here is also a link to test the code above:

https://www.jdoodle.com/iembed/v0/s7F

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文