What I am trying to do is to read a file and store the data into an array. The array that it chooses depending on the amount of numbers on each line in the file. I keep coming up with out of bounds for the index. What am I doing wrong, and is my code correct for what I am trying to do?
For the file I am reading it is something like this.
>2.0 5.0 3.5
>
>5.2 0.5 4.8
>
>1.0
>
>2.5
I want the lines that have 3 numbers to be stored inside the box array and the lines that have 1 number to be stored inside the ball array. However, I want to be able to store any amount of lines into the arrays not just these 4.
This is my code so far.
import java.io.*;
public class MainProg{
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("info.txt");
BufferedReader br = new BufferedReader(fr);
String nums;
int count =0;
int lineNo = 0;
while((nums = br.readLine()) != null){
String numbers[] = nums.split(" ");
double[][] ball = new double[lineNo][];
if(numbers.length == 3){
for(int i = 0; i < numbers.length; i++){
double[][] box = new double[lineNo][i];
box[lineNo][i] = Double.parseDouble(numbers[lineNo]);
lineNo++;
System.out.println(box[i] + " ");
}
}else{
while(numbers.length == 1 && ((nums = br.readLine()) != null)){
int p = 0;
ball[count][p] = Double.parseDouble(numbers[count]);
p++;
count++;
}
}
}
}
}
I'm supposed to do this in an array of objects but I want to figure out how to do it this way first.
pcould be replaced with a hard-coded0. Yes,pis occasionally incremented to1, but that value is never read before it goes out of scope.>characters, or were they an attempt at block-quote formatting?