Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.charAt(Unknown Source)
at DNAmatch.findFirstMatchingPosition(DNAmatch.java:100)
at DNAmatch.run(DNAmatch.java:82)
at acm.program.Program.runHook(Program.java:1568)
at acm.program.Program.startRun(Program.java:1557)
at acm.program.Program.start(Program.java:808)
at acm.program.Program.start(Program.java:1279)
at acm.program.Program.main(Program.java:1360)
我遇到了这个奇怪的错误,我不知道是什么导致了它。我用谷歌搜索解决我的问题,但没有任何帮助我解决它。 如果您尝试作为chain1 atcg和chain2 tagaagtc的输入,它可以正常工作 但对于输入chain1 atcg和chain2 tagaagct,我得到了这个错误。 如果有人可以提供帮助,我会感激它。
=============================================== ==
import acm.program.*;
public class DNAmatch extends Program
{
public void run()
{
//The chains must only contain the characters A,T,C or G so we check the string for any irregularities.
String current, chain2; Boolean flag=true; int errorCount;
String chain1 = readLine("Please type in the first chain of DNA code: ");
for (int i=0; i<chain1.length(); i++)
{
current = chain1.charAt(i) +"";
if (!((current.equalsIgnoreCase("a"))||(current.equalsIgnoreCase("t"))||
(current.equalsIgnoreCase("c"))||(current.equalsIgnoreCase("g"))))
{
flag=false; break;
}
}
while (flag==false)
{
errorCount=0;
println("The DNA code you insert must only contain the characters A, T, C and G");
chain1 = readLine("Please type again in the first chain of DNA code: ");
for (int i=0; i<chain1.length(); i++)
{
current=chain1.charAt(i) +"";
if (!((current.equalsIgnoreCase("a"))||(current.equalsIgnoreCase("t"))||
(current.equalsIgnoreCase("c"))||(current.equalsIgnoreCase("g"))))
{
errorCount =1; break;
}
}
if (errorCount==0)
{
flag=true; break;
}
}
chain2 = readLine("Please type in the second chain of DNA code: ");
flag=true;
for (int i=0; i<chain2.length(); i++)
{
current = chain2.charAt(i) +"";
if (!((current.equalsIgnoreCase("a"))||(current.equalsIgnoreCase("t"))||
(current.equalsIgnoreCase("c"))||(current.equalsIgnoreCase("g")))&&(chain1.length()>chain2.length()))
{
flag= false; break;
}
}
while ((flag==false)&&(chain1.length()>chain2.length()))
{
errorCount=0;
if (chain1.length()>chain2.length())
println("The second DNA chain must be longer or equal to the first one at the most!");
else
println("The DNA code you insert must only contain the characters A, T, C and G");
chain2 = readLine("Please type again the second chain of DNA code: ");
for (int i=0; i<chain2.length(); i++)
{
current = chain2.charAt(i) +"";
if (!((current.equalsIgnoreCase("a"))||(current.equalsIgnoreCase("t"))||
(current.equalsIgnoreCase("c"))||(current.equalsIgnoreCase("g")))&&(chain1.length()>chain2.length()))
{
errorCount =1; break;
}
}
if (errorCount==0)
{
flag=true;
}
}
int match=findFirstMatchingPosition(chain1,chain2);
if (match==-1)
println("Not match found! "+ match);
else
println("Match has been found at point "+ match+ "!");
}
public int findFirstMatchingPosition(String shortDNA, String longDNA)
{
String currentCharShort=""; String currentCharLong="";
int match=0; int ans=-1; int a;
for (int i=0; i < longDNA.length(); i++)
{
a = i;
match = 0;
for (int j=0; j < shortDNA.length(); j++)
{
currentCharLong=longDNA.charAt(a)+ "";
currentCharShort=shortDNA.charAt(j)+ "";
if ( (currentCharShort.equalsIgnoreCase("a") && currentCharLong.equalsIgnoreCase("t"))||
(currentCharShort.equalsIgnoreCase("t") && currentCharLong.equalsIgnoreCase("a"))||
(currentCharShort.equalsIgnoreCase("c") && currentCharLong.equalsIgnoreCase("g"))||
(currentCharShort.equalsIgnoreCase("g") && currentCharLong.equalsIgnoreCase("c")) )
{
match +=1;
a +=1;
}
else break;
}
if (match == shortDNA.length())
{
ans=i;
break;
}
}
return ans;
}
}
答案 0 :(得分:2)
if ( (currentCharShort.equalsIgnoreCase("a") && currentCharLong.equalsIgnoreCase("t"))||
(currentCharShort.equalsIgnoreCase("t") && currentCharLong.equalsIgnoreCase("a"))||
(currentCharShort.equalsIgnoreCase("c") && currentCharLong.equalsIgnoreCase("g"))||
(currentCharShort.equalsIgnoreCase("g") && currentCharLong.equalsIgnoreCase("c")) )
{
match +=1;
a +=1;
}
这就是你的问题所在,因为你嵌套了循环,'a'有机会增加大于longDNA的总长度。
答案 1 :(得分:1)
当您输入此for循环时
private init
与&#34; a&#34;这是你在longDNA中的最后一个字符的索引(例如,如果longDNA有六个字符,则为5),当你输入if分支并输入+ = 1时,你现在有一个大于longDNA大小的索引。因此,当您再次迭代并搜索charAt(a)时,将抛出异常。这也可能发生在较小的&#34; a&#34;如果在内部for循环中执行a + = 1语句不同的时间。