开发者社区> 问答> 正文

如何仅将无空格的行读取CSV文件到HashMap中

我正在执行一个相当独特的CSV文件读取,但是有一个独特的方面。如果该行仅在第19列中具有空白单元格值,则可以进入哈希表。如果该行中的任何其他列具有空白单元格,则应将其从哈希图中排除。该表共有22列。有什么方法可以写此if语句而无需写出每个列索引?例如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;
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 19:04:46 351 0
1 条回答
写回答
取消 提交回答
  • 我最终通过在If语句中逐一列出每个列来完成了很长一段路:

        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;
        }
    

    回答来源:Stack Overflow

    2020-03-26 19:05:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载