简体   繁体   English

Java字符串出界愚蠢

[英]Java string Out of bounds foolishness

I am reading from a file and on one of the lines within the file reads: "{a,b,c,d}" I ran a split function on that line which returned a string array of 10(not sure why 10 instead of 9) elements. 我正在读取一个文件,并在文件中的一行上读取:“{a,b,c,d}”我在该行上运行了一个分割函数,它返回了一个10的字符串数组(不知道为什么10而不是9)元素。 Each element being one character long. 每个元素都是一个字符长。 I then did a foreach loop over that array and within the body of that loop, I am doing a check to see if that "char" is a letterOrDigit and if it is.. do something else. 然后我对该数组进行了一个foreach循环,并在该循环的主体内,我正在检查是否“char”是一个letterOrDigit,如果它是..做其他事情。 But Within the first iteration of that loop, I get a StringIndexOutOfBounds Exception and for the life of me I do not know Why. 但是在该循环的第一次迭代中,我得到一个StringIndexOutOfBounds异常,并且在我的生命中我不知道为什么。 I did not run into this issue while using intelliJ. 使用intelliJ时我没遇到这个问题。 This is being ran in eclipse and it's driving me nuts. 这是在日食中运行,它让我疯了。 Please help. 请帮忙。

 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)));
                }
            }
        }

This is a screenshot of what the String array looks like after the split 这是拆分后String数组的样子截图

This is what the line looks like after the file has been read. 这是读取文件后的行。

The first string in the array is an empty string: "". 数组中的第一个字符串是一个空字符串:“”。 It has a length of 0 so str.charAt(0) is out of bounds. 它的长度为0,因此str.charAt(0)超出范围。

I think you don't need to use spilt function, you want to read whole stuff char by char just like array access. 我认为你不需要使用spilled函数,你想通过char读取整个char char,就像数组访问一样。 Empty value to string split function produces extra element because spilt function has no special case handling for such case, although it does handle case properly when length of split string is 1 字符串拆分函数的空值产生额外的元素,因为溢出函数没有针对这种情况的特殊情况处理,尽管它在拆分字符串的长度为1时正确处理了大小写

You can rewrite your code something thing below 您可以在下面重写您的代码

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)));
                }
            }
        }
    }

From your screenshot, the array element at the index 0's length is 0, ie it's a empty String, but during iteration you're trying to access the first char of empty String, so it throws the IndexOutOfBoundException . 从截图中,索引0长度的数组元素为0,即它是一个空字符串,但在迭代期间,您尝试访问空字符串的第一个字符,因此它会抛出IndexOutOfBoundException

To avoid this check the length or isEmpty before accessing it's char 为了避免这种情况,在访问它的char之前检查lengthisEmpty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM