此错误很难调试。它也不是经常发生的。请帮忙吗? substring
方法是否可能被破坏?
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 32
at java.lang.String.substring(String.java:1765)
at mmk.basej.main(basej.java:186)
Error in line: "tmp1 = filePathShort1.substring(0, 10)"
这是我的代码:
String tmp = null;
String pdf_name = null;
for (int z = 0; z < list_chek.length; z++) {
if (list_chek[z].toString().indexOf("pdf") > 0 | list_chek[z].toString().indexOf("tif") > 0 | list_chek[z].toString().indexOf("jpg") > 0) {
String filePath1 = new String();
String filePathShort1 = new String();
String tmp1 = null;
filePath1 = dir + list_chek[z].toString();
filePathShort1 = list_chek[z].toString();
tmp1 = filePathShort1.substring(0, 10)
+ filePathShort1.substring(20, filePathShort.indexOf("tsd7")-1)
+ "_"
+ filePath1.substring(
filePath1.indexOf("tsd7"), filePath1
.length() - 4);
if (name_no_time.equals(tmp1)) {
//System.out.println(name_no_time+" ---- "+tmp1);
System.out.println("PDF_NAME " + list_chek[z].toString());
pdf_name = list_chek[z].toString();
}
else if ((list_chek[z].toString().indexOf("jpg") > 0) && ((name_no_time + "_1").equals(tmp1))) {
System.out.println("JPG_NAME " + list_chek[z].toString());
pdf_name = list_chek[z].toString();
}
}
}
答案 0 :(得分:0)
filePathShort1 = list_chek[z].toString();
tmp1 = filePathShort1.substring(0, 10)
如果list_chek [z] .toString的长度大于&lt; 10,然后substring(0, 10)
比提供的String长。这是您的索引超出范围的地方。它试图引用超出tmp1
范围的元素。
如果list_chek [z]&lt;你将在下一行中遇到同样的问题。 20.在调用子字符串之前,您需要检查字符串长度。