-2

I wrote this program to read a cvs file I was given. The file has 921 rows and 11 columns. After reading the file, how do I make it an array and then find the averages of the 11 columns? Help would be much appreciated, thanks!

Here are my codes at this point:

package applesa;

import java.io.File;
import java.util.Scanner;

public class readfile {

private Scanner x;

    public void openFile() {
        try{
            x = new Scanner(new File("csvfile"));
        }
        catch(Exception e){
            System.out.println("Could not find file");
        }
    }

    public void readFile(){

        while(x.hasNext()){
            String a = x.next();
            String b = x.next();
            String c = x.next();
            String d = x.next();
            String e = x.next();
            String f = x.next();
            String g = x.next();
            String h = x.next();
            String i = x.next();
            String j = x.next();
            String k = x.next();

            System.out.printf("%s %s %s %s %s %s %s %s %s %s %s\n", a,b,c,d,e,f,g,h,i,j,k);     
        }
    }
    public void closeFile() {
        x.close();
    }
}

With a main method:

package applesa;

public class applesa {
    public static void main (String[] args){

        readfile r = new readfile();
        r.openFile();
        r.readFile();
        r.closeFile();
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122

2 Answers2

0

There are a lot of resources and open posts out there (two are referenced here) that can help with your question.

A better way to read in the csv may be to use the "split" functionality and that already gives you an array of the row: http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/

If you need to set up arrays of the column information you could do something like this:

//for any column:
int[] {col}_array = new int[921];

//for each row read into line:
row = line.split(",");
{col}_array = row[col];

Or with the setup you have above, something like this:

// Set up some type of storage (this is just an example):
int[][] data = new int[11][921];
int[] col_sums = new int[11];  //you need to keep track of each column's sum to calculate the average later
int row_counter = 0;

// Within while(x.hasNext()) look from above
for(int i = 0; i < 11; i++) {      // for each column in current row
 int next_elem = x.next();         // gets the next element of the row like you've been doing
 data[i][row_counter] = next_elem; // sets the data for that column and that row to the new element
 col_sums[i] += next_elem;         // updates the column sums to include this row's data
} // end of for-loop
row_counter++; //increases the row counter for the next iteration of the while loop (i.e. the next row)
//end of while-loop

And to get insight for calculating the average check out this post (the key is in handling ints to get a double for decimal information): How to manipulate arrays. Find the average. Beginner Java

Community
  • 1
  • 1
K.F
  • 459
  • 3
  • 12
0

You can create a String array and assign the values to that:

String[] text = r.readFile();

Then, for your readFile() method:

public String[] readFile() {
    ArrayList<String> text = new ArrayList<>();
    while (x.hasNext()) {
        text.add(x.next());
    }
    return text.toArray();
}
Bethany Louise
  • 646
  • 7
  • 13