StringIndexOutOfBoundsException:String.substring()和String.indexOf()的问题

时间:2017-02-07 02:32:35

标签: java string

我正在编写一个程序来反转句子中的单词顺序。 (例如“红帽属于约翰”=>“约翰属于帽子红色”)我自己走过了do-while循环,但我不明白为什么会有一个索引异常。

感谢您的帮助,

JonBrown

例外:

   Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -23
    at java.lang.String.substring(Unknown Source)
    at WordReverse.main(WordReverse.java:17)

代码:

   public class WordReverse 
    {
        public static void main(String[] args)
        {
            String input = "The red had belongs to John";
            String reverse = "";
            int lastSpace = 0;

            do
            {
            //Isolate Word w/ Preceding Space
            int startIndex = lastSpace;
            int endIndex = input.indexOf(' ', startIndex + 1);

            //Add Word to front of String 
            reverse = input.substring(startIndex, endIndex) + reverse;

            //Add Preceding Space for First Iteration
            if (lastSpace == 0) reverse = " " + reverse;

            //Reset Last Space
            lastSpace = endIndex;

            // Repeat Loop Until line14 .indexOf returns -1 due to lack of " ". 
            }while (lastSpace != -1);

            System.out.println(reverse);
        }
    }

5 个答案:

答案 0 :(得分:0)

来自MSDN String.Substring Method (Int32, Int32)

public string Substring(
    int startIndex,
    int length
)

它应该是这样的:

reverse += input.substring(startIndex, endIndex - startIndex);

答案 1 :(得分:0)

为方便起见,只需使用拆分

    String input = "The red had belongs to John";

    String arr[] = input.split(" ");

    for (int x = arr.length -1; x >= 0; x--) 
         System.out.println(arr[x]);

答案 2 :(得分:0)

这个解决方案更优雅:

public class WordReverse 
{
    public static void main(String[] args)
    {
        String input = "The red had belongs to John";
        String array = input.split(" ");
        for( int i = array.length()-1; i >= 0; i++ )
          System.out.print( array[ i ] + " " );
        System.out.println();
    }
}

答案 3 :(得分:0)

当您到达最后一个空格(“John”之前)时,endIndex值为-1。然后,您尝试使用小于subString()的{​​{1}}来调用endIndex,这会导致崩溃。

答案 4 :(得分:0)

正如其他人所说,你的问题是lastIndex为-1。当发生这种情况时,此修复程序会中断循环,但在将字符串的其余部分复制到结果之前不会:

public class WordReverse 
{
    public static void main(String[] args)
    {
        String input = "The red hat belongs to John";
        String reverse = "";
        int lastSpace = 0;

        do
        {
            //Isolate Word w/ Preceding Space
            int startIndex = lastSpace;
            int endIndex = input.indexOf(' ', startIndex+1);
            if (endIndex==-1) {
                reverse = input.substring(startIndex) + reverse;
                break;
            }

            //Add Word to front of String 
            reverse = input.substring(startIndex, endIndex) + reverse;

            //Add Preceding Space for First Iteration
            if (lastSpace == 0) reverse = " " + reverse;

            //Reset Last Space
            lastSpace = endIndex;

            // Repeat Loop Until line14 .indexOf returns -1 due to lack of " ". 
        }while (true);

        System.out.println(reverse);
    }
}
相关问题