java - 如何将excel文件中的第一行(属性名称)与java中的所有单元格值映射?

标签 java arrays arraylist

我尝试使用以下代码来获取Excel文件中与其他特定单元格值映射的第一行值(属性名称),即属性名称=值(例如年龄=30-39、meno-pause=premeno等)。然后,将映射值添加到ArrayList中。 但没有正确的输出请帮助我!

attribute-name>> age,meno-pause,tumor-size,inv-nodes,node-caps,deg-malig,breast,breast-quard,irradiat,Class

--------***

值(value)观>>

30-39,premeno,30-34,0-2,no,3,left,left_low,no,no-recurrence-events
40-49,premeno,20-24,?,no,2,right,right_up,no,no-recurrence-events
40-49,premeno,20-24,0-2,no,2,left,left_low,no,no-recurrence-events
60-69,ge40,15-19,0-2,no,2,right,left_up,no,no-recurrence-events
40-49,premeno,0-4,0-2,no,2,right,right_low,no,no-recurrence-events
60-69,ge40,15-19,0-2,no,2,left,left_low,no,no-recurrence-events
50-59,premeno,25-29,0-2,no,2,left,left_low,no,no-recurrence-events
60-69,ge40,20-24,0-2,no,1,left,left_low,no,no-recurrence-events
40-49,premeno,50-54,0-2,no,2,left,left_low,no,no-recurrence-events
40-49,premeno,20-24,0-2,no,2,right,left_up,no,no-recurrence-events

Java 代码

public class Excel {

    private static Scanner sc;
    public static void main(String[] args) throws FileNotFoundException {
        List<String> transactions = new ArrayList<String>();
        String str1,str2=null;
        String addArray = null;
        sc = new Scanner(new File("bc_dataset.csv"));
        str1=sc.nextLine();
          while (sc.hasNextLine())
            {
                String str2 = sc.nextLine();
                transactions.add(str1 + "=" + str2);
                System.out.println(transactions);
            }
    }

最佳答案

检查以下更改。解释在评论中。

public class Excel {

        private static Scanner sc;
        public static void main(String[] args) throws FileNotFoundException {
            List<String> transactions = new ArrayList<String>();
            String str1,str2=null;
            String addArray = null;
            sc = new Scanner(new File("bc_dataset.csv"));
            str1=sc.nextLine();
            String []attributes = str1.split(","); // split the attributes by `,`. This will return array of String [age,meno-pause,tumor-size,...]
              while (sc.hasNextLine())
                {
                    String str2 = sc.nextLine();
                    String []linesplit = str2.split(","); // split the line by `,`. This will return array of String [30-39,premeno,30-34....]
                    // loop over both arrays and add them together 
                    for(int i = 0; i<attributes.length; i++){
                       finalline = finalline+attributes[i]+"="+linesplit[i]+",";
                    }
                    // this will add extra `,` in the end of string which I leave on you to escape 
                    transactions.add(finalline); // add final line to the list
                    System.out.println(transactions);
                }
        }

关于java - 如何将excel文件中的第一行(属性名称)与java中的所有单元格值映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929608/

相关文章:

java - 是否有可能优雅地提供来自 Guice 辅助注入(inject)工厂的不包含参数或等效项的实例?

java - 如何使用zuul将响应主体提取到后置过滤器中

JAVA WOPI : What should be the return type of GetFileContent API?

c++ - 如何将5个值的数组1D打印为列?

jquery - 使用 JQuery 将 JSON 对象解析为数组

java - 选中 ExpandableListView (Android) 中的所有复选框

php - 为什么我的 PHP 函数没有产生任何输出?

java - 有效比较两个 ArrayList

java - 展平嵌套数组。 ( java )

java - 线程 "main"java.lang.NullPointerException 中的异常 - Json 到 Java