2016-11-28 106 views
-1
ID  Number 
ssk , 0 
ssk , 999999 
ssk , 0 
oit , 999999 
dnp , 54 
dnp , 2 
dnp , 999999 

上面是我正在閱讀的csv文件縮短版本的一個示例。我的意圖是檢查給定行中是否有0,如果是,則將ID的所有外觀替換爲空字符串。如果不是0,只保留最小值的ID。我的當前程序如何更改以獲得輸出結果?我願意接受任何解決方案,我一直在我的工作崗位上爲此工作數小時。謝謝。在二維數組中搜索並替換多個值Java

想輸出

ID  Number 
    , 0 
    , 999999 
    , 0 
oit , 999999 
    , 54 
dnp , 2 
    , 999999 

我目前的計劃是如下:=============================== =====================

 import java.io.*; 
     import java.lang.reflect.Array; 
     import java.util.*; 

     public class ImapSync{ 

      public static void main(String[] args) throws IOException { 
       FileReader fr = new FileReader("Sync report.csv"); 
       Scanner sc = new Scanner(fr); 
       String[][] array = new String [101000][2]; 
       int i = 0; 

       while(sc.hasNext()){ 
        String line = sc.nextLine(); 
        String field[] = line.split(","); 
        //   System.out.println(field[5]); 
        for(int j = 0; j < 2; j++){ 
         //    System.out.println(field[5] + " " + i); 
         array[i][j] = field[j]; 
        } 
        i++; 
       } 
       sc.close(); 
       Commands c = new Commands(array); 

       c.changeArrayValues(); 
     //  c.print(); 
       c.arrayToFile(); 
      } 




     } 


    import java.io.File; 
    import java.io.IOException; 
    import java.io.PrintWriter; 
    import java.lang.reflect.Array; 
    import java.util.Arrays; 

    public class Commands { 
     private String array[][]; 

     public Commands(String array[][]){ 
      this.array= array; 
     } 
     public void print(){ 
      for(int i = 0; i < array.length; i++){ 
       for(int j = 0; j < 2; j++){   
        if(j == 0) 
         System.out.printf("%-70s " , array[i][j]); 
        else 
         System.out.printf("%-20s ", array[i][j]); 
       } 
       System.out.println(); 
      } 
     } 

     public void arrayToFile() throws IOException{ 
      File f = new File("C:/Users/abc.csv"); 
      PrintWriter p = new PrintWriter(f); 

      for(int i = 0; i < 100981; i++){ 
       for(int j = 0; j < 2; j++){ 
        p.print(array[i][j] + ","); 
       } 
       p.println(); 
      } 
      p.close(); 
     } 

     public void changeArrayValues(){ 
      int arrayPosition = 0; 
      while(arrayPosition < 100981){ 
       if(Integer.parseInt(array[arrayPosition][1]) == 0){ 
        String userID = array[arrayPosition][0]; 

        for(int i = 0; i < 100981; i++){ 
         if(array[i][0] == userID){ 
          array[arrayPosition][0] = ""; 
         } 
        } 
       } 
       arrayPosition++; 
      } 
     } 

    } 
+3

而你的問題是... –

回答

0

與以下替換現有的「changeArrayValues」的方法,讓我知道,它是否適合你:

public void changeArrayValues(){ 
    int arrayPosition = 0; 
    while(arrayPosition < array.length){ 
      if(array[arrayPosition][1] != null && Integer.parseInt(array[arrayPosition][1].trim()) == 0){ 
       String userID = array[arrayPosition][0].trim(); 
       for(int i = 0; i < array.length; i++){ 
        if(array[i][0] != null && array[i][0].trim().equals(userID)){ 
         array[i][0] = ""; 
        } 
       } 
      }else if (array[arrayPosition][0] != null && !array[arrayPosition][0].equals("")) { 
       String userID2 = array[arrayPosition][0].trim(); 
       int min = Integer.parseInt(array[arrayPosition][1].trim()); 
       for (int j=0;j<array.length;j++){ 
        if (array[j][0] != null && array[j][1] != null){ 
         if(array[j][0].equals(userID2) && Integer.parseInt(array[j][1].trim()) < min){ 
          min = Integer.parseInt(array[j][1].trim()); 
          array[arrayPosition][0] = ""; 
         } else if (array[j][0].equals(userID2) && Integer.parseInt(array[j][1].trim()) > min) 
          array[j][0] = ""; 
        } 
       } 
     } 

     arrayPosition++; 
    } 
} 
+0

我收到一個空指針在這一行:if(array [j] [0] .equals(userID2)&& Integer.parseInt(array [j] [1]) Jim

+0

我已經在上面編輯的代碼中處理了空值。請現在嘗試,讓我知道,如果它適合你。 – Mahipal

+0

完美運作!謝謝。 – Jim

0

我真的可以看到200的方式來做到這一點:)

List<String> list = new ArrayList<String>(); 
String holdKey = "The most strange value you can get"; 
String[] splited = new String[2]; 
boolean zeroFound; 

while ((line = br.readLine()) != null) 
    list.add(ch); 

for(int i=list.size()-1;i>=0;i--) { 
    splited = list[i].split(","); 

    if (!holdKey.equals(splited[0]) {   //checks key changes 
     //previus item has to be updated, and previus is +1 
     list[i+1] = (zeroFound?" ":holdKey) + ", " + splited[1];  
     holdKey = splited[0]; 
    } 

    //check if value is zero, after ofc. 
    zeroFound |= "0".equals(trim(splited[1])); 

} 

//Finally finish with the 1st item 
list[0] = (zeroFound?" ":holdKey) + ", " + splited[1]; 

我沒有」運行代碼,所以可能會有一些錯誤...我惱火becouse [tab]沒有在編輯器中工作...