此錯誤很難調試。它也不是經常發生。有人可以幫忙嗎? substring
方法是否可能被破壞?StringIndexOutOfBoundsException:字符串索引超出範圍32 in循環
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();
}
}
}
StringIndexOutOfBounds非常明瞭。 – khelwood
另外,我認爲'tmp1 = filePathShort1.substring(0,10)'是不可能的,因爲[Java API文檔StringIndexOutOfBoundsException](https://docs.oracle.com/javase/7/ docs/api/java/lang/StringIndexOutOfBoundsException.html)_Thrown由String方法指示索引是否定的或大於字符串的大小。對於某些方法(如charAt方法),當索引等於string._的大小時,也會引發此異常。您的錯誤很可能是由於代碼中的另一行。 –
這是關於tmp1計算的東西。 tmp1 = 2017-02-02005002801AFAC_tsd7 and filePathShort1 = 2017-02-02_15-32-46_005002801AFAC_tsd7.pdf – antbug