Java 索引意外越界

Java 索引意外越界,java,Java,我试图制作一个程序,读取一个字符串并将完整的单词放入一个字符串数组中。这是通过避免空间来实现的。这是我的密码: 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)!=' ')

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

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;
        }
    }
String s=“你好”;
字符串str=“”;
int i=0;
字符串strArr[]=新字符串[100];
int j=0;
而(jI)这个循环:

while(s.charAt(j)!=' ')
{
    str = str+s.charAt(j);
    j++;
} 
您正在无任何控制地递增
j
。您应该在
while
循环中添加一个检查:
while(j

附言:如果
S和S的条件相同,你有两个
,我会重构

第2点:
str.equals(null)
如果
str
不是
null
,则始终为
false

p.S.3:您可以使用
String#split
进行此操作-更容易出错。

I此循环:

while(s.charAt(j)!=' ')
{
    str = str+s.charAt(j);
    j++;
} 
您正在无任何控制地递增
j
。您应该在
while
循环中添加一个检查:
while(j

附言:如果
S和S的条件相同,你有两个
,我会重构

第2点:
str.equals(null)
如果
str
不是
null
,则始终为
false


p.S.3:您可以使用
String#split
来实现这一点-更容易出错。

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

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我将首先说我不是专家,但我相信我知道这里出了什么问题

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这段代码有各种各样的错误。若你们只需要一个用空格分隔的单词数组,那个么你们可以使用这个:

String s = "Hello how are you";
String[] words = s.split(" +");
System.out.println(String.join(", ", words));
如果您确实不想使用
拆分
,则这将执行相同的操作。它可能有一个嵌套循环,但由于它们递增相同的计数器,因此仍然是线性时间:

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]);
String s=“你好”;
List words=new ArrayList();
对于(int i=0;i
此代码存在各种各样的错误。若您只需要一个由空格分隔的单词数组,则可以使用此代码:

String s = "Hello how are you";
String[] words = s.split(" +");
System.out.println(String.join(", ", words));
如果您确实不想使用
拆分
,则这将执行相同的操作。它可能有一个嵌套循环,但由于它们递增相同的计数器,因此仍然是线性时间:

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]);
String s=“你好”;
List words=new ArrayList();
对于(int i=0;i
您好,我在您的初始代码中做了一些修改,我相信这是一个最简单的解决方案:

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;
}
String s=“你好”;
字符串str=“”;
int i=0;
字符串strArr[]=新字符串[100];
int j=0;
而(j

这里只需要一个循环来拆分字符串。

您好,我对您的初始代码进行了一些修改,我相信这里有一个最简单的解决方案:

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;
}
String s=“你好”;
字符串str=“”;
int i=0;
字符串strArr[]=新字符串[100];
int j=0;
而(j

这里只需要一个循环来分割字符串。

您的主要问题是内部
while
循环中的条件。您需要确保您没有越界

此外,您的
str=null;
行需要更改为
str=“”;
,否则您将在结果中获得字符串
“null”

您的原始解决方案,只需稍作修改即可修复:

String s=“你好”;
字符串str=“”;
int i=0;
字符串strArr[]=新字符串[100];
int j=0;

while(j你的主要问题是内部
while
循环中的条件。你需要确保你没有越界

此外,您的
str=null;
行需要更改为
str=“”;
,否则您将在结果中获得字符串
“null”

您的原始解决方案,只需稍作修改即可修复:

String s=“你好”;
字符串str=“”;
int i=0;
字符串strArr[]=新字符串[100];
int j=0;

虽然jI尝试了这个解决方案,但它仍然显示出相同的错误。首先检查时?在
s.charAt(j)!='
之前?它指向if条件
while(s.charAt(j)!='&&j
中存在的while循环,所以你没有检查我的解决方案…已经用
s.charAt(j)之前的检查询问了!=''
…我分别对if条件和while循环应用了检查。我尝试过此解决方案,但它仍然显示相同的错误。首先检查时?在
s.charAt(j)之前!=''
?它指向if条件中存在的while循环
wh