Search code examples
javavectorio

Splitting the input into vectors


I must split this input from file into vectors and add to a vectors.

File input

    1,375,seller
   1,375,sellers
   1,375,send
   1,375,sister
   1,375,south
   1,375,specific
   1,375,spoiler
   1,375,stamp
   1,375,state
   1,375,stop
   1,375,talked
   1,375,tenant
   1,375,today
   1,375,told

    FileInputStream fstream = new FileInputStream("e://inputfile.txt");
        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        while ((strLine = br.readLine()) != null)  
        {
            Vector dataPoints = new Vector();
            dataPoints.add(br);


             dataPoints.add(new DataPoint());
        }


   ------ public DataPoint(double x, double y, String name) this is the method

How to split the string into double and string and give a input to the vector?


Solution

  • Use String#split(String):

    Vector<DataPoint> dataPoints = new Vector<DataPoint>();        
    while ((strLine = br.readLine()) != null) {
        String[] array = strLine.split(",");
        dataPoints.add(new DataPoint(Double.parseDouble(array[0]), Double.parseDouble(array[1]), array[2]));
    }