How to read CSV file into HashMap only for rows without blanks

FinDev :

I'm performing a fairly unique CSV file reading but there is one unique aspect. If the row has a blank cell value only in the 19th column, it is OK to go into the hashmap. If any other column in the row has a blank cell, it should be excluded from the hashmap. The table has 22 total columns. Is there any way to write this if statement without writing out each and every column index? For example if(piecesOfInfo[0] != "" && piecesOfInfo[1] != ""...)

public HashMap<String,String[]> createHashMap() {
    File flightFile = new File("fying.csv");
    HashMap<String,String[]> flightsMap = new HashMap<String,String[]>();

    try {
    Scanner s = new Scanner(flightFile);
    while (s.hasNextLine()) {
            String info = s.nextLine();
            String [] piecesOfInfo = info.split(",");
            if(/**piecesOfInfo contains no blanks aside from the 18th column**/){
            String flightKey = piecesOfInfo[4]; //Setting the Key
            String[] values = Arrays.copyOfRange(piecesOfInfo, 0, piecesOfInfo.length);

            flightsMap.put(flightKey, values);
            }
            }

    s.close();
    }


   catch (FileNotFoundException e)
   {
     System.out.println("Cannot open: " + flightFile);
   }

    return flightsMap;
}
FinDev :

I ended up doing it the long way by listing out each column individually in the If statement:

    public HashMap<String,String[]> createHashMap() {
        File flightFile = new File("flights.csv");
        HashMap<String,String[]> flightsMap = new HashMap<String,String[]>();

        try {
        Scanner s = new Scanner(flightFile);
        while (s.hasNextLine()) {

                String info = s.nextLine();
                String [] piecesOfInfo = info.split(",");
                if(piecesOfInfo[0] != "" || piecesOfInfo[1] != "" ||    piecesOfInfo[2] != "" ||    
                   piecesOfInfo[3] != "" ||     piecesOfInfo[4] != "" ||    piecesOfInfo[5] != "" ||    
                   piecesOfInfo[6] != "" ||     piecesOfInfo[7] != "" ||    piecesOfInfo[8] != "" ||    
                   piecesOfInfo[9] != "" ||     piecesOfInfo[10] != "" ||   piecesOfInfo[11] != "" ||   
                   piecesOfInfo[12] != "" ||    piecesOfInfo[13] != "" ||   piecesOfInfo[14] != "" ||   
                   piecesOfInfo[15] != "" ||    piecesOfInfo[16] != "" ||   piecesOfInfo[17] != "" ||   
                   piecesOfInfo[19] != "" ||    piecesOfInfo[20] != "" ||   piecesOfInfo[21] != "") {
                    String flightKey = piecesOfInfo[4] + "_" + piecesOfInfo[2] + "_" + piecesOfInfo[11]; //Setting the Key
                    String[] values = Arrays.copyOfRange(piecesOfInfo, 0, piecesOfInfo.length);

                    flightsMap.put(flightKey, values);
                }

        }
        s.close();
        }


       catch (FileNotFoundException e)
       {
         System.out.println("Cannot open: " + flightFile);
       }

        return flightsMap;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297488&siteId=1