user2149122 user2149122 - 1 year ago 42
Java Question

Read float from a line throws ArrayIndexOutOfBoundsException

I have a .txt file in the format of an integer followed by a float and they are separated by the space character in every line. I want to read the float from every line and then put it in an array.
Here is my code so far, but when I run it it gives me a


because I guess it never creates the second value:

BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(new FileInputStream(
new File(root.getAbsolutePath().toString()+"/samplefile.txt"))));
String line = null;
String[] numbers = null;
int i = 0;
while((line = reader.readLine()) != null){
numbers = line.split("/\\s/");
}catch(IOException e){

So how can I go to second value?

Answer Source

Try with:

numbers = line.split("\\s+");