我尝试使用以下代码来获取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/