为什么for循环最终以DNA [2]最终出现。
我一直在研究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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于
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
对于
您的代码,请扔
StringIndexoutofBoundSexception
exception&amp;因此,您认为它不再前进到下一个循环的迭代:为了避免这种情况,您可以在调用substring之前检查startIndex始终小于oferindex:
For
Your code is throwing
StringIndexOutOfBoundsException
exception & hence you sees it is no longer advancing to next iteration of for loop :To avoid this kind of scenario, you can check startIndex is always less than stopIndex before calling substring:
问题在于您编写
findsimplegene
方法的方式,特别是当您尝试检索stopindex
时。实际上,在此上下文中3)您可能超出给定字符串
的长度。另外,计算stopIndex
startIndex
可能是-1毫无意义。您可以在计算之前添加支票,例如三元运算符,该操作员控制
startIndex
不是-1,并且会计3仍然使其在给定的String
中。附带说明,仅仅为了减少代码,您就可以将结果
设置为“不存在”,并且仅在可以进行子字符串(较少IF-ELSE分支和更好的可读性)时才将其更新。输出
也是测试上述代码的链接:
https://www.jdoodle.com/ iembed/v0/s7f
The problem lies in the way you've written your
findSimpleGene
method, specifically when you try to retrievestopIndex
. In fact, in that contextstartIndex
could be at the end of the string or less than 3 characters before the end, so when you're performingdna.indexOf("taa", startIndex + 3)
you might be exceeding the length of the givenString
. Also, computingstopIndex
whenstartIndex
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 givenString
. On a side note, just to reduce your code, you could already setresult
to "Not Exist" and update it only if the substring can take place (less if-else branching and better readability).Output
Here is also a link to test the code above:
https://www.jdoodle.com/iembed/v0/s7F