-1

I want to read this csvFile into an array of Flight class objects in which each index will refer to an object containing a record from the csvFile.

Here is a blueprint of Flight class. Its not complete so I am only providing the data memebers.

public class Flight {
  private String flightID;
  private String source;
  private String destination;
  private <some clas to handle time > dep;
  private <some clas to handle time> arr;
  private String[] daysOfWeek;
  private <some clas to handle date> efff;
  private <some clas to handle date> efft;
  private <some clas to handle dates> exc;

}

I want to implement a function something like :

public class DataManager {



public List<Flight> readSpiceJet() {
    return new ArrayList<Flight>(); 
}

}

Feel free to modify this and please help me. :)

Thanks in advance.

eltabo
  • 3,749
  • 1
  • 21
  • 33
  • 2
    yes we will feel free.. but can you show us your current implementation of this.. the ones you have tried and havent worked? – Shreyas Sarvothama Oct 19 '16 at 10:12
  • I could not implement this thing I tried cleaning the csv data using openCSV library but that didn't helped me in achieving my goal to create an Array of objects. All it returned was Strings not objects. – Mohit Kumar Oct 19 '16 at 10:16
  • 1
    Might be a good idea to post your code that you mentioned didn't help you. Someone may be able to spot where you went wrong. Much better than asking someone to give you a solution from scratch. – Adrian Sanguineti Oct 19 '16 at 10:17
  • please post the code that did not work for you – Blip Oct 19 '16 at 10:21
  • `CSVReader spicejetreader = new CSVReader(new FileReader("data/2016.spicejet.csv"),'|','\'',4);` `String[] nextLine; while ((nextLine = spicejetreader.readNext()) != null) { System.out.println(nextLine[2]); }` this reurns a string which i am unable to parse into a object @Blip @Adrian – Mohit Kumar Oct 19 '16 at 10:30
  • Edit your question and post the code in the question – Blip Oct 19 '16 at 10:36

1 Answers1

1

You can try OpenCSV Framework.

Have a look at this example:

import java.io.FileReader;
import java.util.List;

import com.opencsv.CSVReader;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvToBean;

public class ParseCSVtoJavaBean 
{
    public static void main(String args[])
    {
        CSVReader csvReader = null;

        try
        {
            /**
             * Reading the CSV File
             * Delimiter is comma
             * Default Quote character is double quote
             * Start reading from line 1
             */
            csvReader = new CSVReader(new FileReader("Employee.csv"),',','"',1);
            //mapping of columns with their positions
            ColumnPositionMappingStrategy mappingStrategy = 
                    new ColumnPositionMappingStrategy();
            //Set mappingStrategy type to Employee Type
            mappingStrategy.setType(Employee.class);
            //Fields in Employee Bean
            String[] columns = new String[]{"empId","firstName","lastName","salary"};
            //Setting the colums for mappingStrategy
            mappingStrategy.setColumnMapping(columns);
            //create instance for CsvToBean class
            CsvToBean ctb = new CsvToBean();
            //parsing csvReader(Employee.csv) with mappingStrategy  
            List empList = ctb.parse(mappingStrategy,csvReader);
            //Print the Employee Details
            for(Employee emp : empList)
            {
                System.out.println(emp.getEmpId()+"   "+emp.getFirstName()+"   "
                        +emp.getLastName()+"   "+emp.getSalary());

            }
        }
        catch(Exception ee)
        {
            ee.printStackTrace();
        }
        finally
        {
            try
            {
                //closing the reader
                csvReader.close();
            }
            catch(Exception ee)
            {
                ee.printStackTrace();
            }
        }
    }
}

EDIT 1:

To parse dates:

String dateString;
Date date;

public void setDateString(String dateString) {
 // This method can parse the dateString and set date object as well
}

public void setDate(Date date) {
     //parse here
}
Pang
  • 9,564
  • 146
  • 81
  • 122
arnabkaycee
  • 1,634
  • 13
  • 26
  • can you also suggest how to parse the date and time parts of the records while reading into the date time fileds. – Mohit Kumar Oct 19 '16 at 10:27
  • You might want to create a dateString object in your class and have its setter parse the date as per your requirement. – arnabkaycee Oct 19 '16 at 10:33
  • please post the well indented code this code is very confusing. Also it can be because i am new into java @arnabkaycee – Mohit Kumar Oct 19 '16 at 10:36