字符串索引超出范围错误:如何修复?

时间:2022-12-11 20:25:52

I am trying to mix up a string using string buffers. The program should randomly select a character within the word that is in StringBuffer1 (sb1). Append it to the empty StringBuffer2 (sb2), and continuously do this until the sb1.length() of the StringBuffer1 (sb1) is equal to 0 (empty).

我试图使用字符串缓冲区混合一个字符串。程序应该随机选择StringBuffer1(sb1)中单词中的一个字符。将它附加到空StringBuffer2(sb2),并持续执行此操作,直到StringBuffer1(sb1)的sb1.length()等于0(空)。

Here is my code:

这是我的代码:

String word = "Hello";
String empty = "";
StringBuffer sb1 = new StringBuffer(word);
StringBuffer sb2 = new StringBuffer(empty);
Random randomChar = new Random();

while (word.length() != 0) {
    int charIndex = randomChar.nextInt(word.length());
    char character = sb1.charAt(charIndex);
    sb2.append(character);
    sb1.deleteCharAt(charIndex);
}

System.out.println(word.length());
System.out.println(word);
System.out.println(sb2);

2 个解决方案

#1


0  

You are making changes in StringBuffer sb1 whereas you are validating the loop against length of String word. So.

您正在对StringBuffer sb1进行更改,而您正在根据String字的长度验证循环。所以。

  1. You are creating an infinite loop, and
  2. 你正在创建一个无限循环,和

  3. Random number is generated based on word.length(), which will be 5 always.
  4. 随机数是基于word.length()生成的,总是为5。

Use sb1.length() instead of word.length() so that length of sb1 is used for loop as well as random number.

使用sb1.length()而不是word.length(),以便sb1的长度用于循环以及随机数。

#2


-1  

Try this. There might be possibility that this may loop more times than expected because of Random.

试试这个。由于Random,这可能会比预期循环更多次。

Explanation : Issue was with the index. When you are selecting random from word length you will get element from 0 -> length-1 but at the same time you are removing element from string buffer as well so next time when you will get index=length-1 it won't be present in string buffer that's why you are getting error. Also I have added terminated condition when string buffer length becomes zero.

说明:问题与索引有关。当您从字长中选择随机时,您将从0 - > length-1获取元素,但同时您也要从字符串缓冲区中删除元素,因此下次当您获得index = length-1时它将不会存在于字符串缓冲区中,这就是您收到错误的原因。当字符串缓冲区长度变为零时,我还添加了终止条件。

            String word = "Hello";
            String empty = "";
            StringBuffer sb1 = new StringBuffer(word);
            StringBuffer sb2 = new StringBuffer(empty);
            Random randomChar = new Random();

            while (word.length() != 0 && sb1.length() != 0) {
                int charIndex = randomChar.nextInt(word.length());
                if (sb1.length() > charIndex) {
                    char character = sb1.charAt(charIndex);
                    sb2.append(character);
                    sb1.deleteCharAt(charIndex);
                }
            }

            System.out.println(word.length());
            System.out.println(word);
            System.out.println(sb2);

#1


0  

You are making changes in StringBuffer sb1 whereas you are validating the loop against length of String word. So.

您正在对StringBuffer sb1进行更改,而您正在根据String字的长度验证循环。所以。

  1. You are creating an infinite loop, and
  2. 你正在创建一个无限循环,和

  3. Random number is generated based on word.length(), which will be 5 always.
  4. 随机数是基于word.length()生成的,总是为5。

Use sb1.length() instead of word.length() so that length of sb1 is used for loop as well as random number.

使用sb1.length()而不是word.length(),以便sb1的长度用于循环以及随机数。

#2


-1  

Try this. There might be possibility that this may loop more times than expected because of Random.

试试这个。由于Random,这可能会比预期循环更多次。

Explanation : Issue was with the index. When you are selecting random from word length you will get element from 0 -> length-1 but at the same time you are removing element from string buffer as well so next time when you will get index=length-1 it won't be present in string buffer that's why you are getting error. Also I have added terminated condition when string buffer length becomes zero.

说明:问题与索引有关。当您从字长中选择随机时,您将从0 - > length-1获取元素,但同时您也要从字符串缓冲区中删除元素,因此下次当您获得index = length-1时它将不会存在于字符串缓冲区中,这就是您收到错误的原因。当字符串缓冲区长度变为零时,我还添加了终止条件。

            String word = "Hello";
            String empty = "";
            StringBuffer sb1 = new StringBuffer(word);
            StringBuffer sb2 = new StringBuffer(empty);
            Random randomChar = new Random();

            while (word.length() != 0 && sb1.length() != 0) {
                int charIndex = randomChar.nextInt(word.length());
                if (sb1.length() > charIndex) {
                    char character = sb1.charAt(charIndex);
                    sb2.append(character);
                    sb1.deleteCharAt(charIndex);
                }
            }

            System.out.println(word.length());
            System.out.println(word);
            System.out.println(sb2);