java - 从一行中读取 float 抛出 ArrayIndexOutOfBoundsException

标签 java android indexoutofboundsexception

我有一个 .txt 文件,格式为一个整数后跟一个 float ,它们在每一行中由空格字符分隔。我想从每一行读取 float ,然后把它放在一个数组中。 到目前为止,这是我的代码,但是当我运行它时,它给了我一个 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+");

关于java - 从一行中读取 float 抛出 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914306/

相关文章:

java - 乘以 double 时索引超出范围

c# - Index was outside the bounds of the array 异常

java - Bean单例、 session 创建和初始化

java - 如何使用designgridlayout在页面中的两类数据之间插入垂直JSeperator?

java - .NET 中 java.net.URLConnection 的等效项

java - 如何在 java 中使用特定规则对给定字符串进行子字符串化

java - 您可以将 Spriter Pro 2d 装配 Sprite 导入 LibGDX 吗?

java - Android 二进制 xml 文件错误膨胀类/无法调用回收的实例

深度导航中的 Android 抽屉导航

android - 如何获得图像的缩放功能?