Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.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 从一行读取浮点抛出ArrayIndexOutOfBoundsException_Java_Android_Indexoutofboundsexception - Fatal编程技术网

Java 从一行读取浮点抛出ArrayIndexOutOfBoundsException

Java 从一行读取浮点抛出ArrayIndexOutOfBoundsException,java,android,indexoutofboundsexception,Java,Android,Indexoutofboundsexception,我有一个.txt文件,格式是一个整数,后跟一个浮点,每行用空格分隔。我想从每一行读取浮点,然后将其放入一个数组中。 这是到目前为止我的代码,但当我运行它时,它会给我一个ArrayIndexOutOfBoundsException 因为我想它永远不会创造第二个值: BufferedReader reader = null; try{ reader = new BufferedReader(new InputStreamReader(new F

我有一个.txt文件,格式是一个整数,后跟一个浮点,每行用空格分隔。我想从每一行读取浮点,然后将其放入一个数组中。 这是到目前为止我的代码,但当我运行它时,它会给我一个
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。使用调试器可以更好地理解代码的问题