【问题标题】:StringIndexOutOfBoundsException: String index out of range 32 in loopStringIndexOutOfBoundsException:循环中的字符串索引超出范围 32
【发布时间】:2017-02-02 14:28:02
【问题描述】:

这个错误很难调试。它也不会经常发生。有人可以帮忙吗? 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();
                    }               
                }
            }

【问题讨论】:

  • StringIndexOutOfBounds 不言自明。
  • 另外,我认为tmp1 = filePathShort1.substring(0, 10) 不可能对您的错误负责,因为从Java API documentation StringIndexOutOfBoundsException 由字符串方法抛出以指示索引为负数或大于大小的字符串。对于某些方法,例如charAt 方法,当索引等于字符串的大小时也会抛出此异常。。您的错误很可能是由于代码中的另一行造成的。
  • 这是关于 tmp1 计算的东西。 tmp1 = 2017-02-02005002801AFAC_tsd7 和 filePathShort1 = 2017-02-02_15-32-46_005002801AFAC_tsd7.pdf

标签: java for-loop substring


【解决方案1】:
filePathShort1 = list_chek[z].toString();
tmp1 = filePathShort1.substring(0, 10)

如果 list_chek[z].toString 的长度 substring(0, 10) 比提供的字符串长。这是您的索引越界的来源。它试图引用和元素超出tmp1 的范围。 如果 list_chek[z]

【讨论】:

  • 所以如果 filePathShort1 = 2017-02-02_15-32-46_005002801AFAC_tsd7.pdf 和 tmp1 必须像:2017-02-02005002801AFAC_tsd7 我必须使用 tmp1 = filePathShort1.substring(0, 9) 代替?
  • 字符串有固定大小:filePathShort1 = 2017-02-02_15-32-46_005002801AFAC_tsd7.pdftmp1=2017-02-02005002801AFAC_tsd7
  • 你确定它们的长度都一样且足够长吗?您可能需要插入调试语句进行检查。它有时有效而不是其他有效,并且由于该异常而在该线上失败的事实强烈表明它们还不够长。
  • 其他一些东西:| 是二进制 or,我想你想要逻辑的,|| 你也可以用.contains("pdf") 代替.indexOf("pdf") &gt; 0
  • 字符串足够长。我只有大文件列表才有这个错误。然后我将该列表分成小部分,程序在它们上运行良好。
猜你喜欢
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多