ID Number
ssk , 0
ssk , 999999
ssk , 0
oit , 999999
dnp , 54
dnp , 2
dnp , 999999
上面是我正在阅读的csv文件的缩短版本的示例。我的目的是检查给定行中是否有0,如果是,则将ID的所有外观替换为空String。如果不是0,只保留最小值的ID。如何更改当前程序以获得所需的输出?我对任何和所有的解决方案持开放态度,我一直在我的工作地点工作了好几个小时。谢谢。
想要输出
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++;
}
}
}
答案 0 :(得分: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];
我没有运行代码所以可能存在一些错误......我感到恼火因为[tab]在编辑器中不起作用...
答案 1 :(得分: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++;
}
}