0

I am making an android app that reads in CSV files that contain values for the amount of people in parks. My aim is to make an app that outputs statistical analysis of the data such as means, modes and charts. My problem is I am unsure of how to store the data in java. There could be any number of rows but there will always be 72 columns. As you can see from below I used a simple array but this obviously doesn't work and only stores the last observation. I was thinking maps but unsure how to implement it. Any help is very much appreciated.

package messy;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Playing
{
    static String[] tokens;
    public static void main(String[] args)
    {
    //Input file which needs to be parsed
    String fileToParse = "C:/Users/acer/Desktop/soparc shit/SOPARC.csv";
    BufferedReader fileReader = null;

    //Delimiter used in CSV file
    final String DELIMITER = ",";
    try
    {
        String line = "";
        //Create the file reader
        fileReader = new BufferedReader(new FileReader(fileToParse));
         fileReader.readLine();
        //Read the file line by line
        while ((line = fileReader.readLine()) != null) 
        {
            //Get all tokens available in line
            tokens = line.split(DELIMITER);

        }
    } 
    catch (Exception e) {
        e.printStackTrace();
    } 
    finally
    {
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   }
}
3
  • Maybe store them in a List<String[]>, for example an ArrayList. Commented Aug 26, 2014 at 11:05
  • See this SO question stackoverflow.com/questions/14114358/… Commented Aug 26, 2014 at 11:17
  • Exactly what I wanted adrian. perfect. thanks Commented Aug 26, 2014 at 12:01

3 Answers 3

1

You can use a CSV Parser, such as OpenCSV

CSVReader reader = new CSVReader(new FileReader(fileToParse));  
String[] recordArray = null;

while ((recordArray = reader.readNext()) != null) {
    // use recordArray...
}
Sign up to request clarification or add additional context in comments.

1 Comment

An example of how to use OpenCSV to parse a CSV in Java can be found here stackoverflow.com/a/18068820/181406
0

You need to use a Map like this:

Map<String, List<String>> values

A very good and detailed example is here:

Reading CSV file in Java and storing the values in an int array

Comments

0

Apart from OpenCSV, already mentioned, see also:

Comments

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.