Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java字符串越界愚蠢_Java_Arrays_String_Intellij Idea_Indexoutofboundsexception - Fatal编程技术网

Java字符串越界愚蠢

Java字符串越界愚蠢,java,arrays,string,intellij-idea,indexoutofboundsexception,Java,Arrays,String,Intellij Idea,Indexoutofboundsexception,我正在从文件中读取,文件中的一行显示: “{a,b,c,d}”我在那一行运行了一个split函数,它返回一个包含10个元素的字符串数组(不确定为什么是10而不是9个元素)。每个元素有一个字符长。然后我在这个数组上做了一个foreach循环,在这个循环的主体中,我正在做一个检查,看看这个“char”是否是一个letterDigit,它是否是。。做点别的。但在该循环的第一次迭代中,我得到了一个StringIndexOutOfBounds异常,我不知道为什么。我在使用intelliJ时没有遇到这个问题

我正在从文件中读取,文件中的一行显示: “{a,b,c,d}”我在那一行运行了一个split函数,它返回一个包含10个元素的字符串数组(不确定为什么是10而不是9个元素)。每个元素有一个字符长。然后我在这个数组上做了一个foreach循环,在这个循环的主体中,我正在做一个检查,看看这个“char”是否是一个letterDigit,它是否是。。做点别的。但在该循环的第一次迭代中,我得到了一个StringIndexOutOfBounds异常,我不知道为什么。我在使用intelliJ时没有遇到这个问题。这是在eclipse中运行的,它快把我逼疯了。请帮忙

 for(int line = 1; line < file.size(); line++) // start at line 1 cause the line with the alphabet is useless.
    {
        if(line == 1) // Line 1 is always the states... so create the states.
        {
            String[] array = file.get(line).split("");
            for(String str :array) // split returns a string Array. each element is is one char long
            {
                // we only care about the letters and not the '{' or the ','
                if (Character.isLetterOrDigit(str.charAt(0))) {
                    dfa.addState(new State(str.charAt(0)));
                }
            }
        }
for(int line=1;line


数组中的第一个字符串是空字符串:“”。它的长度为0,因此str.charAt(0)超出范围。

从您的屏幕截图中,索引0长度处的数组元素为0,即它是一个空字符串,但在迭代过程中,您试图访问空字符串的第一个字符,因此它会抛出
IndexOutOfBoundException


为了避免这种情况,请在访问
char

之前检查
length
isEmpty
,我认为您不需要使用spilt函数,您需要像数组访问一样逐字符读取整个内容。 字符串拆分函数的空值会产生额外的元素,因为拆分函数对这种情况并没有特殊的大小写处理,尽管当拆分字符串的长度为1时,它会正确地处理大小写

你可以在下面重写你的代码

for (int line = 1; line < file.size(); line++) // start at line 1 cause the line with the alphabet is useless.
    {
        if (line == 1) // Line 1 is always the states... so create the states.
        {
            String lineValue = file.get(line);
            int len = lineValue.length();
            for (int index = 0; index < len; index++) {
                if (Character.isLetterOrDigit(lineValue.charAt(index))) {
                    //dfa.addState(new State(str.charAt(0)));
                }
            }
        }
    }
for(int line=1;line
好的。我将添加该检查。但我不知道为什么第一个元素开头是空字符串。在我的文件中不是这样的。我添加了另一个屏幕截图,显示了该行在读取后的实际外观。您是否考虑过使用
string.getChars()
来获取字符,而不是使用
string.split()
?这将避免所有字符串大小检查。我从未想过这一点。但我认为该函数需要多个参数。我不确定是否必须始终能够调用这些参数,我认为拆分会更容易。它应该像
char[]arr=new char[line.size()];line.getChars(0,line.size())一样简单,arr,0)
哦,是的,那也行。我想我会这样做的。那个拆分函数让我疯狂,只是随机添加了一个空字符串。最奇怪的是,我在intelliJ中编程时没有遇到这个问题。谢谢!