Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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 StringIndexOutOfBoundsException_Java_Stringindexoutofbounds - Fatal编程技术网

读取子字符串时遇到Java StringIndexOutOfBoundsException

读取子字符串时遇到Java StringIndexOutOfBoundsException,java,stringindexoutofbounds,Java,Stringindexoutofbounds,所以我有一个java类,它读取一个有多行的文件,每当它到达这个部分 F C Y 它应该读取它的值(返回F,C,Y) 我尝试过ClassString=temp.substring(0,20.trim() 但它总是返回StringIndexOutOfBoundsException(eString索引超出范围:20) 不知道为什么,我从一开始就数了那一行,Y在索引20中。我会这样做,当然可能需要一些错误检查 temp = temp.trim(); String[] resul

所以我有一个java类,它读取一个有多行的文件,每当它到达这个部分

      F    C    Y
它应该读取它的值(返回F,C,Y)

我尝试过ClassString=temp.substring(0,20.trim()

但它总是返回StringIndexOutOfBoundsException(eString索引超出范围:20)


不知道为什么,我从一开始就数了那一行,Y在索引20中。

我会这样做,当然可能需要一些错误检查

temp = temp.trim();
String[] result = new String[] {String.valueOf(temp.charAt(0)),
                                temp.substring(1, temp.length()-2).trim(), 
                                String.valueOf(temp.charAt(temp.length()-1))};
或者作为字符数组

temp = temp.trim();
char[] result2 = new char[] {temp.charAt(0),
                             temp.substring(1, temp.length()-2).trim().charAt(0), 
                             temp.charAt(temp.length()-1)};

先生,总长度或温度为17
temp.子字符串(0,17)。trim()
适用于您的情况。