2014-03-07 22 views
1

我有一個程序可以讀取一個csv文件。 csv文件有兩列,一列用於簇號,另一列用於用戶。我所需要的程序不是重複用戶。相反,要向用戶寫入一次,並在每個羣集中寫入1(如果用戶已在該羣集中顯示)。如何讓用戶在閱讀csv文件時出現一次?

包解析;

public static void main(String[]args){ 

     String fileName= "ClusterResult(final1).csv";//reading the file 
     File file = new File(fileName); 
     try { 
      Scanner inputStream = new Scanner(file); 
     while (inputStream.hasNext()){ 
       String data = inputStream.next(); 
       String [] myArray= data.split(","); 

       for(int i =0; i<=27;i++){// because I have 27 clusters    
        Arrays.sort(myArray); 
        int founditem= Arrays.binarySearch(myArray, String.valueOf(i)); 

        if (founditem>-1)  
         System.out.print("1"); //if the user name showed in the cluster the user should have one next to the cluster number 

        else 
         System.out.print("0"); 

       } 
       System.out.println(); 
       System.out.println(myArray[1]); 
     } 
     inputStream.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 

程序的輸出是:

1000000000000000000000000000 

ElDaddy 

1000000000000000000000000000 

Lxve 

1000000000000000000000000000 

Lxve 

0000001000000000000000000000 

ElDaddy 

,它應該是例如:

1000001000011000010100000000 
ElDaddy 
+1

不是很清楚'cluster'的東西。顯示一個輸入示例? – PeterMmm

+0

你可以發佈csv的樣本嗎? – rpax

回答

0

你說你的問題中您的CSV有兩列,如果是這樣,請嘗試如下所示:

public static void main(String[] args) { 

     HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>(); 
     String fileName = "ClusterResult(final1).csv";// reading the file 
     FileReader file = null; 
     try { 
      file = new FileReader(new File(fileName)); 
     } catch (FileNotFoundException e1) { 
      System.err.println("File " + fileName + " not found!"); 
      e1.printStackTrace(); 
     } 
     BufferedReader br = new BufferedReader(file); 
     String line; 
     try { 
      while ((line = br.readLine()) != null) { 
       String[] data = line.split(","); 
       generateClusters(data[0], data[1], map); 
      } 
      for (Entry<String, HashSet<String>> entry : map.entrySet()) { 

       for (int i = 0; i < 27; i++) { 
        if (entry.getValue().contains(String.valueOf(i))) 
         System.out.print(1); 
        else 
         System.out.print(0); 
       } 
       System.out.println(); 
       System.out.println(entry.getKey()); 
      } 

     } catch (IOException e) { 
      System.err.println("Error when reading"); 
      e.printStackTrace(); 
     } finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException e) { 
        System.err.println("Unexpected error"); 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

    public static void generateClusters(String e1, String e2, 
      HashMap<String, HashSet<String>> map) { 

     HashSet<String> clustersOfUser = map.get(e1); 

     if (clustersOfUser == null) { 
      clustersOfUser = new HashSet<String>(); 
        map.put(e1,clustersOfUser); 
     } 
     clustersOfUser.add(e2); 
    } 

我們這裏做的是分組在HashMap的用戶,但存儲集羣 作爲鍵。

+0

非常感謝你的工作 – user3391350

+0

@ user3391350很高興提供幫助。請標記爲已接受 – rpax