import java.io.*;
import java.util.*;
//================================================================================
// Read any size CSV file into a 2 dimensional array
//================================================================================
public class ReadCSVintoArray
{    
    public static void main(String[ ] args)
    {
//      String input_file = "zAnalytics/dataset1.csv";
        String input_file = "/home/sultans/web/java/demo/8inpout/zAnalytics/dataset1.csv";
    
        if (args.length > 0)                                    //if argument is provided 
            input_file = args[0];                               //use it as an input file

        String[][] dataPoints = readFile(input_file);           //read the file into a 2 dim array

        display(dataPoints);                                    //display entire 2 dim array
    }

//--------------------------------------------------------------------------------
// Read the input CSV file, and store into a 2 dimensional String array
//--------------------------------------------------------------------------------
    static String[][] readFile(String filename)
    { 
        int nRows = 0;                                  //number of lines/rows in file
        int nCols = 0;                                  //number of columns for each line
        String[]   dim1Array = null;                    //1 dim array to hold all the cols for each row
        String[][] dim2Array = null;                    //2 dim array to hold all the data points

        System.out.println("SOURCE INPUT...");
        try 
        {
            FileReader       fr  = new FileReader(filename);        //open the file for reading
            LineNumberReader lnr = new LineNumberReader(fr);        //use to get the number of lines in file

            lnr.skip(Long.MAX_VALUE);                               //skip to end of file
            nRows = lnr.getLineNumber();                            //obtain the num of lines      
            lnr.close();                                            //close the file                                      

            dim2Array = new String[nRows][];                //create a 2 dim array with as many rows as lines in file 

            File    f    = new File(filename);              //create a file object               
            Scanner file = new Scanner(f);                  //scanner for the input file

            int row=0;
            while (file.hasNextLine())                      //while there are lines in the file
            {
                String line = file.nextLine();              //use scanner to get next line
                System.out.println(line);                   //print it
                dim1Array       = line.split(",");          //split it on ,
//              dim1Array       = line.split("\\s*,\\s*");  //split it on any space , any space
                dim2Array[row]  = dim1Array;                //append the 1 dim col array into the 2 dim rows array
                row++;                                      //next row
            }
        }            
        catch (Exception e)
        {
            System.out.println(e);
        }
        return(dim2Array);
    }
//--------------------------------------------------------------------------------
// Display entire dataset
//--------------------------------------------------------------------------------
    static void display(String[][] dataset)
    {
        System.out.println();
        System.out.println("2 DIMENSIONAL ARRAY...");

        for (String[] row : dataset)
        {
            for (String col : row)                          //loop through all the columns
                System.out.printf("%8s ", col);             //print     
            System.out.println();
        }
    }
//---------------------------------------------------------------------------------
}