[英]Java Check Duplicates in data file and add number
I have data file like:
First Name,Last Name,Number
Robert,Thomas
Jimmy,Johns
Robert,Thomas
Conner,Thomas
Robert,Thomas
Jimmy,Johns
Outfile Should be like:
First Name,Last Name,Number
Robert,Thomas,1
Robert,Thomas,2
Robert,Thomas,3
Jimmy,Johns,1
Jimmy,Johns,2
Conner,Thomas,
我寫了代碼,但實際上是刪除了那些重復的數據,但我想在最后一個字段中寫數字。
這是我的代碼:
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
public class Data_Prospect_key1 {
public static void delFileFromDir(String dirPath) {
File dir = new File(dirPath);
if (dir.listFiles() == null)
return;
for (File file : dir.listFiles()) {
if (!file.isDirectory())
file.delete();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
BufferedReader br = null;
BufferedWriter bfAlldcount = null;
File folderall = new File("Data_File_IN");
File[] BFFileall = folderall.listFiles();
for (File file : BFFileall) {
String str = file.getName();
br = new BufferedReader(new FileReader(file));
String count = "Data_File_OUT\\" + "OUT_" + str;
bfAlldcount = new BufferedWriter(new FileWriter(count));
String line;
line = br.readLine();
Map<String, String> maps = new HashMap<String, String>();
while ((line = br.readLine()) != null) {
String[] actionID = line.split("\\,");
String PFN = actionID[0].trim();
String PLN = actionID[1].trim();
maps.put(PFN, PLN);
}
for (Map.Entry<String, String> entry : maps.entrySet()) {
System.out.println("First Name: -" + entry.getKey() + " , Last Name="
+ entry.getValue());
}
}
} catch(Exception lettercount) {
lettercount.printStackTrace();
}
}
}
我的代碼輸出只是刪除重復項。輸出是:
Robert,Thomas
Jimmy,Johns
Robert,Thomas
請幫助我,我該怎么做..請..謝謝在Advaced!
這里有一些指針
像這樣。
package se.wederbrand.stackoverflow;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CountingRobert {
public static void main(String[] args) {
Map<String, Integer> names = new HashMap<>();
Scanner scanner;
if (args.length == 0) {
// for testing
String input = "First Name,Last Name,Number\n" +
"Robert,Thomas\n" +
"Jimmy,Johns\n" +
"Robert,Thomas\n" +
"Conner,Thomas\n" +
"Robert,Thomas\n" +
"Jimmy,Johns";
scanner = new Scanner(input);
}
else {
scanner = new Scanner(args[0]);
}
// Skip one line with the heading
scanner.nextLine();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (!names.containsKey(line)) {
names.put(line, 0);
}
names.put(line, names.get(line) + 1);
}
for (String name : names.keySet()) {
for (int i = 1; i <= names.get(name); i++) {
System.out.println(name + " " + i);
}
}
}
}
我對該方法有些困惑。 您的文件包含以下格式:
Robert,Thomas
Jimmy,Johns
Robert,Thomas
Conner,Thomas
Robert,Thomas
Jimmy,Johns
並且您想要獲取名字和姓氏的列表(無重復)。
因此,您可以逐行讀取文件,然后將FS,PS放入Map中並按照此過程進行操作,直到EOF。由於MAP具有不存在重復Key的屬性,因此您將獲得所需的輸出
Robert,Thomas
Jimmy,Johns
Conner,Thomas
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.