索引超出范围

时间:2019-09-12 14:58:04

标签: java

我试图制作一个程序,该程序可以读取字符串并将完整的单词放入字符串数组中。这是通过避免空间来完成的。这是我的代码:

l1

字符串数组应包含String s ="Hello how are you"; String str = ""; int i=0; String strArr[] = new String[100]; int j=0; while(j<=s.length()-1) { if(s.charAt(j)!=' ') { while(s.charAt(j)!=' ') { str = str+s.charAt(j); j++; } } j++; if(!str.equals(null)) { strArr[i++] = str; str=null; } } Hellohoware。但 它显示you,但我找不到为什么它会这样?

5 个答案:

答案 0 :(得分:1)

此代码很容易出错。.如果您只想将单词数组用空格隔开,则可以使用以下代码:

String s = "Hello how are you";
String[] words = s.split(" +");
System.out.println(String.join(", ", words));

如果您真的不想使用split,那么它将执行相同的操作。它可能有一个嵌套循环,但由于它们要递增相同的计数器,所以它仍然是线性时间:

String s = "Hello how are you";

List<String> words = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
    // increment until you find the start of a word, a non-space
    if (s.charAt(i) != ' ') {
        int wordStart = i;
        // increment to the end of the word, a non-space or end of string
        while ((i < s.length()) && (s.charAt(i) != ' ')) { i++; }
        // add the word to your list
        words.add(s.substring(wordStart, i));
    }
}

System.out.println(words);

// if you really need it as an array
String[] wordsArray = words.toArray(new String[0]);

答案 1 :(得分:1)

您好,我在您的初始代码中做了一些修改,我相信这是最简单的解决方案:

String s ="Hello how are you";
String str = "";
int i = 0;

String strArr[] = new String[100];
int j = 0;

while(j < s.length()) {
    if(s.charAt(j) == ' ') {
        if (!str.isEmpty()) {
            strArr[i++] = str;
        }
        str = "";
    } else {
        str += s.charAt(j);
    }

    j++;
}
if (!str.isEmpty()) {
    strArr[i++] = str;
}

这里只需要一个循环就可以拆分字符串。

答案 2 :(得分:0)

我这个循环:

while(s.charAt(j)!=' ')
{
    str = str+s.charAt(j);
    j++;
} 

您在没有任何控制的情况下递增j。您应该在while循环中添加一个检查:while(j < s.length() && s.charAt(j) != ' ')

P.S .:您有2个ifwhile个条件相同,我会进行重构。

P.S.2:如果str.equals(null)不是false,则str将始终为null

P.S.3:您可以使用String#split-容易出错。

答案 3 :(得分:0)

首先,我不是专家,但我相信我知道这里出了什么问题。

while(j<=s.length()-1)              
{       
    if(s.charAt(j)!=' ')
    {
        while(s.charAt(j)!=' ') //Will constantly be true as long as the previous if statement is true
        {
            str = str+s.charAt(j);
            j++; //
        }           
    }

    j++;
    if(!str.equals(null))
    {
        strArr[i++] = str;
        str=null;
    }
}

基本上,您在第二个while循环中不断增加j。 我建议改用for循环:

for(j = 0; j < s.length(); j++) {
    if(s.charAt(j)!=' ') {
            str = str+s.charAt(j);
        }
    if(!str.equals(null) {
        strArr[i++] = str;
        str=null;
    }
}

如果有人发现此问题,请告诉我,我将予以纠正。

答案 4 :(得分:0)

您的主要问题是内部while循环中的条件。您需要确保您不会超出限制。

另外,您的str=null;行需要更改为str="";,否则结果中将得到字符串"null"

您的原始解决方案,已进行了较小的更改:

String s ="Hello how are you";
String str = "";
int i=0;

String strArr[] = new String[100];
int j=0;


while(j<=s.length()-1)
{
  if(s.charAt(j)!=' ')
  {
    while(j<=s.length()-1 && s.charAt(j)!=' ') // Added a check here
    {
      str = str+s.charAt(j);
      j++;
    }
  }

  j++;
  if(!str.equals(null))
  {
    strArr[i++] = str;
    str=""; // Changed this line
  }
}

注意:我不想为您重构代码,但我同意其他评论者的意见;您本可以使用更具可读性和抗错误性的算法来处理此问题。我会把它留给您解决,因为这不是您要的。