我正在读取一个包含员工的登录和注销值的文本文件,并尝试在单行机器人中显示同一员工和日期在不同行中的输入和输出值。该文本文件中的数据如图所示以下。
line 1. 02,-"sangeetha-May 02, 2013 , -in-09:48:06:61
line 2. 01,-"lohith-May 01, 2013 , -out-09:10:41:61
line 3. 02,-"sushma-May 02, 2013 , -in-09:48:06:61
line 4. 01,-"sushma-Jan 01, 2013 , -in-09:07:06:50-out-05:39:01:63
line 5. 02,-"sangeetha-May 02, 2013 , -out-08:08:19:27
line 6. 02,-"sushma-May 02, 2013 , -out-07:52:13:51
line 7. 03,-"lohith-May 03, 2013 , -in-11:39:44:08
示例:第 1 行和第 5 行是同一员工 sangeetha 的输入和输出值,因此应显示如下:
02,-"sangeetha-May 02, 2013 , -in-09:48:06:61, -out-08:08:19:27
也许我得到了这个输出,但是第 2 行,它没有价值,所以我的代码无法显示只有哪个员工具有不同的输入和输出值,就像它显示那些一样。我还想显示这些记录,并附有丢失的消息。我的代码是这样的。
public class RecordParser {
public static void main(String[] args) {
RecordParser rp = new RecordParser();
rp.recordFormatter("sample.txt");
}
public static void recordFormatter(String filename) {
try {
BufferedReader in;
List<String> ls = new ArrayList<String>();
String line = "";
String line1;
String line2;
String lines;
String mergedRecords = "";
String normalRecords = "";
String halfRecords = "";
in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
ls.add(line);
}
for (int i = 0; i < ls.size(); i++) {
line1 = ls.get(i);
if (line1.contains("in") && line1.contains("out")) {
normalRecords += line1;
normalRecords += System.getProperty("line.separator");
// ls.remove(i);
// break;
}
for (int j = i + 1; j < ls.size(); j++) {
line2 = ls.get(j);
if (line2.contains("in") && line2.contains("out"))
continue;
if (line1.contains(getNameDate(line2))) {
mergedRecords += line1
+ line2.substring(line2.lastIndexOf(","), line2.length());
mergedRecords += System.getProperty("line.separator");
// ls.remove(i);
// ls.remove(i);
break;
}
if (!line1.contains(getNameDate(line2))) {
if (!mergedRecords.contains(getNameDate(line1))) {
halfRecords += line1;
halfRecords += System.getProperty("line.separator");
}
}
}
}
System.out.println(mergedRecords);
System.out.println(normalRecords);
System.out.println(halfRecords);
// && line2.contains("out") && line1.contains("in")
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static String getNameDate(String input) {
return input.substring(0, input.lastIndexOf(","));
}
}
请任何人都可以修改它以显示只有输入或输出的记录吗?
例如..第 2 行应显示如下:
line 2. 01,-"lohith-May 01, 2013 ,missing in, -out-09:10:41:61..
目前我得到的输出是:
02,-"sangeetha-May 02, 2013 , -in-09:48:06:61, -out-08:08:19:27
02,-"sushma-May 02, 2013 , -in-09:48:06:61-out-07:52:13:51
01,-"sushma-Jan 01, 2013 , -in-09:07:06:50-out-05:39:01:63
我希望第 2 行记录与这些一起显示。