A.Rodarte
A.Rodarte

Reputation: 15

Error reading a file and storing data into multiple arrays

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.

Upvotes: 1

Views: 44

Answers (1)

Wasi Ahmad
Wasi Ahmad

Reputation: 37691

Your code has several logical problem.

When the value of lineNo and i is zero, what does the following lines mean? (try to think)

double[][] ball = new double[lineNo][];
double[][] box = new double[lineNo][i];

Why are you increasing the variable lineNo inside the for loop instead of while loop?

for(int i = 0; i < numbers.length; i++){
     ...
     lineNo++;
     ...
}

Why there is a while loop inside the else block?

while(numbers.length == 1 && ((nums = br.readLine()) != null)){
    int p = 0;
    ball[count][p] = Double.parseDouble(numbers[count]); 
    p++;
    count++;
}

Why you are reading in this while loop using BufferedReader br? These doesn't make sense.

Other problems are,

  • Does your input contain empty lines as you provided in the example of input?
  • You want to store values in ball and box array but you don't know how many lines are there in the input file? Then how can you create an array without knowing the size? I rather suggest you to use ArrayList to do your task.

For example, you can do something like following.

BufferedReader br = new BufferedReader(new FileReader("info.txt"));
ArrayList<Double> ball = new ArrayList<>();
ArrayList<Double[]> box = new ArrayList<>();

String nums;
while((nums = br.readLine()) != null){      
    String numbers[] = nums.split(" ");
    if(numbers.length == 3){
        Double[] box_row = new Double[numbers.length];
        for(int i = 0; i < numbers.length; i++){
            box_row[i] = Double.parseDouble(numbers[i]);
        }
        box.add(box_row);
    }else if(numbers.length == 1){
        ball.add(Double.parseDouble(numbers[0]));
    }
}

System.out.println("Box Array");
for(Double[] element: box){
    System.out.println(Arrays.toString(element));
}
System.out.println("Ball Array");
System.out.println(ball.toString());

I am assuming your input file contains no empty line and no > character. The above code snippet outputs the following.

Box Array
[2.0, 5.0, 3.5]
[5.2, 0.5, 4.8]
Ball Array
[1.0, 2.5]

Upvotes: 1

Related Questions