0

I have a text file with 12 columns. Each column has a title and then a float or a string in the column, for example

A           B        time
-0.042    centre   00:00:03

Each column is separated by a space. I would like to compute the average of the numbers in each column. Later on I will compute the average of the columns for certain time intervals and compare it to the overall average. So I figured it might be easiest to put all the elements in an array and call this array the title of the column. I am unsure of how to make several arrays or lists, one for each column and how to skip a column if it only has strings. I'm using a try catch for the text file. Here is what I have for my try so far:

try {
    FileReader fileReader = new FileReader(fileName);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    ArrayList<Integer> lines =  new ArrayList<Integer>();
    Float columns = 12;
    while((num_line = bufferedReader.readLine()) != null) {
      lines.add(Float.valueOf(line.split(" ")[columns-1]));
    }
  for(int l:lines){
    sum+=l;
  }
  float average = (float)sum/lines.size();
bufferedReader.close();     
}
Bree
  • 1
  • You will get error for Float.valueOf(line.split(" ")[columns-1]) when there is a string you need to have a try catch around it. – StackFlowed Feb 20 '17 at 20:44
  • What have you tried to solve the second part of your question "...how to skip a column if it only has strings"? – Philipp Merkle Feb 20 '17 at 21:08

1 Answers1

0

I am unsure of how to make several arrays or lists, one for each column

You could use a List of columns (or rows), where each column (or row) is again represented by a List:

List<List<Integer>> columns = new ArrayList<>();

This post provides more infomation on working with a list of lists: Working with a List of Lists in Java

Community
  • 1
  • 1
Philipp Merkle
  • 2,555
  • 2
  • 11
  • 22