JAVA报错,大家帮看看怎么改
public class Base643
{
public static final String BASE64CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static final String BASE64DECODE = "ABCDEGHIJKLMQRSTUVWXYZabcdefghijlmnopqrstwxyz0123456789.";
public static final int SPLIT_LINES_AT = 76;
public Base643()
{
}
public static String encodebook(String s, int i, int j)
{
String s1 = s;
if(s == null || s.length() < 10){ s1 = ""; }
StringBuffer stringbuffer = new StringBuffer();;
for(int l=0;l < s.length()/ 2;l++){
stringbuffer.append(s.substring(1 + l * 2, 2 + l * 2));
String s2 = stringbuffer.toString();
String s3 = s2.substring(0, i);
String s4;
if(i > 0)
s4 = (new StringBuilder(String.valueOf(s3))).append(".").append(s2.substring(i, s2.length() - j)).append(".").append(s2.substring(s2.length() - j)).toString();
else
s4 = (new StringBuilder(String.valueOf(s2.substring(i, s2.length() - j)))).append(".").append(s2.substring(s2.length() - j)).toString();
s1 = s4;
}
return s1;
}
public static void main(String[] args){ System.out.println("s1 = "+encodebook("kl4ofgsmgeje5gko99s1fc2ofm", 3, 3));}
}
报错结果为
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.substring(String.java:1935)
at com.Base643.encodebook(Base643.java:79)
at com.Base643.main(Base643.java:90)
还有:我不确定for循环那里的条件是for(int l=0;l < s.length()/ 2;l++)还是for(int l=0;l >= s.length()/ 2;l++)
,当改为 >= 时貌似没有进入for?直接输出的是kl4ofgsmgeje5gko99s1fc2ofm
------解决方案--------------------
s.substring(1 + l * 2, 2 + l * 2)
里面的 2 + l * 2 > s.length,所以越界了