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++;
}
}
}
而你的問題是... –