我每次尝试编译异常java.lang.StringIndexOutOfBoundsException
时都出现代码问题。这是有问题的代码,我真的不知道我做错了什么。在代码中,我尝试使用一些条件拆分string
,string
代表一个多项式。Java异常:java.lang.StringIndexOutOfBoundsException
int[] coef1= new int[20];
for(i=0;i<polinom.length()+1;i++){
if(polinom.charAt(i)=='+')
c=polinom.charAt(i+1);
else{
if(polinom.charAt(i)=='^'){
v=Integer.parseInt(Character.toString(polinom.charAt(i+1)));
coef1[v]=Integer.parseInt(Character.toString(c));
System.out.print(coef1[v]);
}
}
}
for(i=0;i<polinom.length()+1;i++){
if(polinom.charAt(i)=='-')
c=polinom.charAt(i+1);
else{
if(polinom.charAt(i)=='^'){
v=Integer.parseInt(Character.toString(polinom.charAt(i+1)));
coef1[v]=-Integer.parseInt(Character.toString(c));
System.out.print(coef1[v]);
}
}
}
唯一的例外是在这里if(polinom.charAt(i)=='+')
张贴堆栈跟踪会更容易跟踪这些异常被抛出 – Ramanlfc
我同意@ Ramanlfc这个错误可以相当容易地解决,如果我们知道它正在发生的线。 – basic
在第二行中,您使用“for(i = 0; i
f1sh