0

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 
1
  • 1
    Hi, of course you tried to debug, right? put the breakpoints into code and run debugger, that will help you to understand what is happening , or start with printing InputLine value eg. , btw due java convention, naming is camel-case starting with small case, so it should be named like inputLine Commented Mar 28, 2019 at 12:41

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.