0

I have a CSV file with different columns of data.

Example:

100, 0.1, 0.5
200, 0.1, 0.1
300, 0.2, 0.5

etc

I want to store the first column into an array, the second column into its own array and so on so that I can use them for math equations.

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

public class Example
{
    public static void main(String[] args) throws Exception 
    {
        Scanner s = new Scanner(new File("C://java//data.txt"));
    }
}

I'm pretty lost up until this point.

8
  • Use csvjdbc.sourceforge.net or something like it. What you are doing is the hard way. Commented Mar 5, 2014 at 0:52
  • So, what you're saying is that you have no idea what to do? Have you thought, in abstract terms, what it is that you want to implement? Why did you instantiate a Scanner - is there any functionality in that class you planned to use but are not telling us? You need to give us something more if you want help. Commented Mar 5, 2014 at 1:11
  • look up String.split() that would help you in what you need. The first split is 2 columns (hint split function have this capability), the second split is to again split the remaining data into array. good luck. Commented Mar 5, 2014 at 1:19
  • What I want to implement is a way to use this data from these columns and plug them into a mathematical equation such as the sum of column1*column3 or something like that. Thank you guys for your answers. I'm still a bit lost but I will try String.split(). Commented Mar 5, 2014 at 2:07
  • Explain a bit more what you mean by "column". Is a column all the rows, or 1 per row? That is, would you expect "100 200 300" to be a single column, or 3 different columns? Commented Mar 5, 2014 at 2:20

1 Answer 1

1
You can try this:

public class ReadCSV {
    public static List<List<Double>> getDimensionList(String key) throws Exception  
    {        
        List<List<Double>> listOfDimension = new ArrayList<List<Double>>();
        List<Double> dimensionList = null;      
        File file = null;
        BufferedReader br = null;
        try {
            file = new File(key);   
            br = new BufferedReader(new FileReader(file));

            String strLine = "";
            StringTokenizer dimensionVal = null;
            int lineNumber = 0, tokenNumber = 0;
            while( (strLine = br.readLine()) != null)
            {
                    dimensionVal = new StringTokenizer(strLine, ",");
                    dimensionList = new ArrayList<Double>();
                    while(dimensionVal.hasMoreTokens())
                    {
                        tokenNumber++;
                        dimensionList.add(Double.parseDouble(dimensionVal.nextToken()));

                        if(tokenNumber == 3)
                        {
                            Comparator<Double> comparator = Collections.reverseOrder();
                            Collections.sort(dimensionList,comparator);

                        }
                    }
                    listOfDimension.add(dimensionList);
                    tokenNumber = 0;
                lineNumber++;
            }
        }catch (Exception e) {
            throw new Exception();
        }
        finally
        {
            if(br != null)
                br.close();         
        }       
        return listOfDimension;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getDimensionList("dimension_details.csv"));
    }
Sign up to request clarification or add additional context in comments.

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.