【问题标题】:Substring with the loop带有循环的子字符串
【发布时间】:2011-05-13 11:59:26
【问题描述】:

在下面的代码中,我试图根据开始和结束索引来做子字符串,但在字符串的末尾。系统正在抛出ArrayIndexOutOfBoundsException。请告诉我,如何解决此问题。

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int initlength = 20;
        int start = 0;
        String s = "Some people confuse acceptance with apathy, but there's all "+
"the difference in the world";
        int total=(int)Math.ceil((double)s.length()/(double)initlength);
        for (int i = 0; i < total; i++) {
            System.out.println("s length" + s.substring(start, initlength));
            start = initlength + 1;
            initlength = initlength + initlength;
            }
    }

问候,

柴图

【问题讨论】:

  • @user569125 : 我已经执行了这段代码并且产生了异常。
  • 所以,呃……你到底想在这里完成什么? 0_0
  • sthupahsmaht:在这里我循环整个字符串并根据 start 和 end.in 上面的代码获取字符串的一部分,我的字符串的总长度是:87,start=0,要检索的长度是20.finally start 变成 81,end lenght 变成 100.But 我想检索 81 到 87 th 字符串?
  • @user569125: 那么,你想读取一个包含 20 个字符块的字符串吗?

标签: java string exception substring


【解决方案1】:

逐步调试您的代码:

  1. 第一次变量

    start=0; initlength=0; s="Some people confuse acceptance with apathy, but there's all the difference in the world"; total = 5.

  2. s.length()/initlength = 4.

  3. 第一个子字符串将从 0 到 20。
  4. start = 21initlenght = 40
  5. 第二次循环
  6. s.length()/initlength = 2
  7. 从 21 到 40 的子字符串。
  8. start = 41initlength = 80
  9. 第三次循环。
  10. s.length()/initlength = 1i 等于 2,因此循环将中断,程序执行将完成。

根据您的编辑。现在它将循环 5 次。并且在第三次之后start = 81initlength = 160 超出了字符串的范围。一直以来total = 5


如果你想要它会得到剩余的部分,试试这个:

    int initlength = 20;
    int start = 0;
    String s = "Some people confuse acceptance with apathy, but there's all "
            + "the difference in the world";
    int total = (int) Math.ceil((double) s.length() / (double) initlength);
    for (int i = 0; i < total; i++) {
        if(initlength<s.length()){
            System.out.println("s length" + s.substring(start, initlength));
            start = initlength + 1;
            initlength = initlength + initlength;
        } else {
            initlength = s.length();
            System.out.println("s length" + s.substring(start, initlength));
            break;
        }
    }

输出:-

s lengthSome people confuse 
s lengthcceptance with apat
s lengthy, but there's all the difference in th
s length world

【讨论】:

  • @Harry Joy:它不起作用。如果我放置 System.out.println("s length" + s.substring(start, initlength));超出 if 和 else 条件
  • @user569125:当然是的,因为接下来会发生同样的事情,即 indexOutOfBuonds,因为您将尝试使用 initlength 进行子字符串化而不更改它。 initlength = s.length();System.out.println("s length" + s.substring(start, initlength)); 之前的行 initlength = s.length(); 是防止 outOfBoundsException 所必需的。 break; 也一样。
  • @Harry Joy:如果我放在最后,即使它不是起始位置为 0 的子字符串。
  • @user569125: 是的,因为您在if 条件下更改start 值,因此如果您将其放在最后,它将产生新的start 值的结果。也不要这样做,因为你不会得到想要的结果,因为在else 中的break; 也不会在最后一部分执行。
【解决方案2】:

initlength 不是会越来越大,直到超过字符串的末尾吗?

【讨论】:

    猜你喜欢
    • 2013-11-02
    • 2012-04-07
    • 2022-01-26
    • 2013-11-08
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多