2017-07-29 17 views
0

是我的工作,到目前爲止,我已經用了2個CSV文件,並取得代表2 CSV文件中的數據,現在我所想要做的是2個不同的陣列打印相同的元素我想比較這2個陣列和打印相同vlaues或元件,其在兩個數組一樣,這是我在哪裏有困難比較在Java 2字符串數組,並從兩個陣列下面

import java.util.Arrays; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

public class CSVReaderw { 

public static void main(String[] args) { 

    String csvFile1 = "/Users/Documents/AD.csv"; 
    String csvFile2 = "/Users/Desktop/database.csv"; 
    BufferedReader br = null; 
    String line = ""; 
    String cvsSplitBy = ","; 
    String[] ad = null; 
    String[] database = null; 

    try { 

     br = new BufferedReader(new FileReader(csvFile1)); 
     while ((line = br.readLine()) != null) { 

      // use comma as separator 
      ad = line.split(cvsSplitBy); 

      //System.out.println("AD [id= " + ad[2] +"]"); 

     } 
     System.out.println("second file result starts here"); 
     br = new BufferedReader(new FileReader(csvFile2)); 
     while ((line = br.readLine()) != null) { 

      database = line.split(cvsSplitBy); 
      //System.out.println("ID =" + database[0]); 
     } 

     List<String> commonListLambda = Arrays.stream(database) 
     .filter(x -> Arrays.asList(ad).contains(x)) 
     .collect(Collectors.toList()); 

     System.out.println(commonListLambda); 
      // List<String> commonList = new ArrayList<>(); 
// 
//    for(int i = 0; i < ad.length; i++){ 
//     if(Arrays.asList(databse).contains(ad[i])) 
//      commonList.add(ad[i]); 
//     } 
//    
//    System.out.println(commonList); 



    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (br != null) { 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

} 

編輯: 看到更新後的代碼,因爲我們dicuessed這裏

+1

的[我如何在Java中比較字符串?]可能的複製(https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)在該問題 –

+0

他們只是顯示如何比較字符串我想comapre數組中的值,並從數組中打印相同的值 – newlearner

+1

'if(ad [i] == database [j])'你正在比較你得到的字符串文件。它完全一樣的解決方案。 –

回答

1

的問題在於你正在重寫你的數組,而不是向它添加數據。

while ((line = br.readLine()) != null) { 
      database = line.split(cvsSplitBy); 

一般的邏輯是錯誤的,你需要在一個文件中比較細胞在其他文件中的單元格。

請參閱建議的解決方案,我試圖使代碼不言自明的。

public class CSVReaderw { 

    public static void main(String[] args) { 

     String adDotCsv = "/Users/Documents/AD.csv"; 
     String databaseDotCsv = "/Users/Desktop/database.csv"; 
     String line = ""; 
     String cvsSplitBy = ","; 
     BufferedReader br = null; 
     List<String> databaseList = new ArrayList<>(); 
     List<String[]> adList = new ArrayList<>(); 

     try { 
      br = new BufferedReader(new FileReader(databaseDotCsv)); 

      while ((line = br.readLine()) != null) { 
       databaseList.addAll(Arrays.asList(line.split(cvsSplitBy))); 
      } 

      br = new BufferedReader(new FileReader(adDotCsv)); 

      line = ""; 
      while ((line = br.readLine()) != null) { 
       adList.add(line.split(cvsSplitBy)); 
      } 

      List<String[]> commonList = new ArrayList<>(); 

      String cellCInAdFile = null; 

      for (String[] rowInAdList : adList) { 
       cellCInAdFile = Arrays.toString(rowInAdList).split(",")[2].trim(); 

       for (String cellAinDatabaseFile : databaseList) { 
        if (cellCInAdFile.equals(cellAinDatabaseFile.trim())) { 
         commonList.add(rowInAdList); 
        } 
       } 
      } 

      for (String[] rowInCommonList : commonList) { 
       System.out.println(Arrays.asList(rowInCommonList)); 
      } 

     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

所以我必須更換我的內心for循環的權利? @ fg78nc – newlearner

+1

我沒有研究注意你的代碼。這是完整的解決方案,它將比較兩個數組並返回通用元素列表。 (int j = 0; j fg78nc

+0

Arrays.asList(廣告)。載(數據庫[I])) commonList.add(數據庫[I]); 的System.out.println(commonList); //值是在兩個陣列中 } } 這是我做的,但它不工作讓我無法找到commonList上的符號 – newlearner