-1

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

      F    C    Y

它应该读取那个值(返回 F、C、Y)

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

但它总是返回 StringIndexOutOfBoundsException (eString index out of range: 20)

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

4

1 回答 1

0

这是我的做法,当然可能需要进行一些错误检查。

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

或作为 char 数组

temp = temp.trim();
char[] result2 = new char[] {temp.charAt(0),
                             temp.substring(1, temp.length()-2).trim().charAt(0), 
                             temp.charAt(temp.length()-1)};
于 2018-04-20T08:20:53.480 回答