Java 从一行读取浮点抛出ArrayIndexOutOfBoundsException
我有一个.txt文件,格式是一个整数,后跟一个浮点,每行用空格分隔。我想从每一行读取浮点,然后将其放入一个数组中。 这是到目前为止我的代码,但当我运行它时,它会给我一个Java 从一行读取浮点抛出ArrayIndexOutOfBoundsException,java,android,indexoutofboundsexception,Java,Android,Indexoutofboundsexception,我有一个.txt文件,格式是一个整数,后跟一个浮点,每行用空格分隔。我想从每一行读取浮点,然后将其放入一个数组中。 这是到目前为止我的代码,但当我运行它时,它会给我一个ArrayIndexOutOfBoundsException 因为我想它永远不会创造第二个值: BufferedReader reader = null; try{ reader = new BufferedReader(new InputStreamReader(new F
ArrayIndexOutOfBoundsException
因为我想它永远不会创造第二个值:
BufferedReader reader = null;
try{
reader = new BufferedReader(new InputStreamReader(new FileInputStream(
new File(root.getAbsolutePath().toString()+"/samplefile.txt"))));
String line = null;
String[] numbers = null;
int i = 0;
value.clear();
while((line = reader.readLine()) != null){
numbers = line.split("/\\s/");
value.add(Float.valueOf(numbers[1].trim()));
}
mTextview5.setText(String.valueOf(value.get(1)));
}catch(IOException e){
e.printStackTrace();
}
那么我如何才能转到第二个值呢?试试:
numbers = line.split("\\s+");
你试过使用调试器吗? 你会很快得到一个结果,程序停止并崩溃 arraylist(您的值)不应抛出ArrayIndexOutOfBoundsException。 我猜你的正则表达式可能有问题
numbers = line.split("/\\s/");
numbers[1] # there is a possibility that your "split regex"
#returns a smaller array than expected, thus ArrayIndexOutOfBoundsException.
试试像这样的regextest你试过这个吗<代码>编号=行。拆分(“”)代码> 问题是您的regexp。使用调试器可以更好地理解代码的问题