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;
}
}
最佳答案
当您输入此for循环时
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;
}
使用“ a”表示您在longDNA中的最后一个字符的索引(如果longDNA有六个字符,则为5),当您输入if分支并放入a + = 1时,您现在拥有的索引大于longDNA的大小。因此,当您再次迭代并搜索charAt(a)时,将引发异常。如果在内部for循环中不同时间执行a + = 1语句,则使用较小的“ a”也可能发生这种情况。