1

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.

2
  • A side issue: p could be replaced with a hard-coded 0. Yes, p is occasionally incremented to 1, but that value is never read before it goes out of scope. Commented Feb 20, 2017 at 4:27
  • Also, does your input file really contain those > characters, or were they an attempt at block-quote formatting? Commented Feb 20, 2017 at 4:30

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

3 Comments

On a related note, why are ball and especially box getting thrown out and recreated over and over? They sure look like they were intended to store something.
I believe, OP is not correctly understanding what he is doing!
Thank you, your reply is really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.