RC07JNR
RC07JNR

Reputation: 595

How do I store the CSV file data into an array in Java?

Here is the CSV file I am using:

B00123,55
B00783,35
B00898,67

I need to read and store the first value entered in the file e.g. B00123 and store it into an array. A user can add to the file so it is not a fixed number of records.

So far, I have tried this code:

public class ArrayReader 
{
    static String xStrPath;
    static double[][] myArray;

    static void setUpMyCSVArray()
    {
        myArray = new double [4][5];

        Scanner scanIn = null;
        int Rowc = 0;
        int Row = 0;
        int Colc = 0;
        int Col = 0;
        String InputLine = "";
        double xnum = 0;
        String xfileLocation;

        xfileLocation = "src\\marks.txt";

        System.out.println("\n****** Setup Array ******");

        try
        {
            //setup a scanner
            /*file reader uses xfileLocation data, BufferedRader uses 
              file reader data and Scanner uses BufferedReader data*/
            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));

            while (scanIn.hasNext())
            {              
                //read line form file
                InputLine = scanIn.nextLine();
                //split the Inputline into an array at the comas
                String[] InArray = InputLine.split(",");

                //copy the content of the inArray to the myArray
                for (int x = 0; x < myArray.length; x++)
                {
                    myArray[Rowc][x] = Double.parseDouble(InArray[x]);
                }
                //Increment the row in the Array
                Rowc++;
            }
        }
        catch(Exception e)
        {

        }
        printMyArray();
    }

    static void printMyArray()
    {
        //print the array
        for (int Rowc = 0; Rowc < 1; Rowc++)
        {
            for (int Colc = 0; Colc < 5; Colc++)
            {
                System.out.println(myArray[Rowc][Colc] + " ");
            }
            System.out.println();
        }
        return;
    }

    public static void main(String[] args)
    {
        setUpMyCSVArray();

    }

}

This loops round the file but doesn't not populate the array with any data. The outcome is:

****** Setup Array ******
[[D@42a57993
0.0 
0.0 
0.0 
0.0 
0.0 

Upvotes: 0

Views: 766

Answers (3)

Naresh
Naresh

Reputation: 119

I use opencsv library to read from csv.

import com.opencsv.CSVReader;

public class CSV {


    private static String file = <filepath>;

    private static List<String> list = new ArrayList<>();

    public static void main(String[] args) throws Exception {
      try {
        CSVReader reader = new CSVReader(new FileReader(file));
        String[] line;
        while ((line = reader.readNext()) != null) {
          list.add(line[0]);
        }
        Object[] myArray = list.toArray();
        System.out.println(myArray.length);
        System.out.println(myArray[0]);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
}

Output printed as below

3
B00123

Upvotes: 0

javapedia.net
javapedia.net

Reputation: 2731

There is actually a NumberFormatException happening when in the first row when trying to convert the ID to Double. So I revised the program and it works for me.

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ArrayReader 
{
    static String xStrPath;
    static Map<String,Double> myArray = new HashMap<>();

    static void setUpMyCSVArray()
    {

        Scanner scanIn = null;
        int Rowc = 0;
        int Row = 0;
        int Colc = 0;
        int Col = 0;
        String InputLine = "";
        double xnum = 0;
        String xfileLocation;

        xfileLocation = "/Users/admin/Downloads/mark.txt";

        System.out.println("\n****** Setup Array ******");

        try
        {
            //setup a scanner
            /*file reader uses xfileLocation data, BufferedRader uses 
              file reader data and Scanner uses BufferedReader data*/
            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));

            while (scanIn.hasNext())
            {              
                //read line form file
                InputLine = scanIn.nextLine();
                //split the Inputline into an array at the comas
                String[] inArray = InputLine.split(",");

                //copy the content of the inArray to the myArray
                    myArray.put(inArray[0], Double.valueOf(inArray[1]));
                //Increment the row in the Array
                Rowc++;
            }
        }
        catch(Exception e)
        {
System.out.println(e);
        }
        printMyArray();
    }

    static void printMyArray()
    {
        //print the array
        for (String key : myArray.keySet()) {
            System.out.println(key + " = " + myArray.get(key));
        }
        return;
    }

    public static void main(String[] args)
    {
        setUpMyCSVArray();

    }

} 

Output:

Output

Upvotes: 3

欧阳葵
欧阳葵

Reputation: 21

the code can't reader anything ,you file path incorrect.give it absoulte file path.

            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));

Upvotes: 1

Related Questions