1
我有一個csv文件,我必須讀取csv文件並分別打印唯一的,重複的和無效的元素。 下面是代碼從csv文件讀取數據並分別打印唯一的,重複的和無效的數據
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashSet;
public class InputData {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\e.csv"));
String line = null;
HashSet<String> lines = new HashSet<>();
HashSet<String> lines1 = new HashSet<>();
System.out.println("Unique List:- ");
while ((line = br.readLine()) != null)
{
{
try {
if (lines.add(line)) {
String[] part = line.split(",");
Integer.parseInt(part[0]);
System.out.println(line);
} else {
lines1.add(line);
}
} catch (NumberFormatException ne) {
System.out.println(" Invalid data:- " + ne);
}
}
}
br.close();
System.out.println("Duplicates:- " +lines1);
}
}
如果輸入
101,Ron,4545,XYZ,3
102,Harry,2345,ABC,3
103,Sam,5448,DEF,3
104,John,9989,GHI,3
101,Ron,4545,XYZ,3
104,John,9989,GHI,3
105,Gang,123,HNB,3
jftdgchj;vcvuigkuj;uygf
hvykfjtucd;gfd;gfd
我得到的輸出: -
Unique List:-
101,Ron,4545,XYZ,3
102,Harry,2345,ABC,3
103,Sam,5448,DEF,3
104,John,9989,GHI,3
105,Gang,123,HNB,3
Invalid data:- java.lang.NumberFormatException: For input string: "jftdgchj;vcvuigkuj;uygf"
Invalid data:- java.lang.NumberFormatException: For input string: "hvykfjtucd;gfd;gfd"
Duplicates:- [101,Ron,4545,XYZ,3, 104,John,9989,GHI,3]
但我想在不同的線路重複...請幫助
'lines1'是HashSet的,和你只是打印出來其默認的toString()行爲。而是循環遍歷設置元素,而不是單獨打印。 – 2015-02-09 23:00:38