1

我正在读取一个包含员工的登录和注销值的文本文件,并尝试在单行机器人中显示同一员工和日期在不同行中的输入和输出值。该文本文件中的数据如图所示以下。

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 行记录与这些一起显示。

4

1 回答 1

0

您的代码最大的问题是它是一团乱麻。很难看出你哪里出错了。

我整理了一些代码来处理您的输入。我编写此代码的原因是向您和其他阅读本文的人展示如何将单体代码分解为可测试的小片段。

不包括我创建的输入字符串(因为你有一个输入文件),这段代码中没有任何方法超过 20 行。有些更短。

我将这段代码运行了三遍。第一次,我忘记编码输出方法了。第二次,我不得不更正输出格式。第三次,我产生了这个输出。

02,-"sangeetha-May 02, 2013 , -in-09:48:06:61 , -out-08:08:19:27
01,-"lohith-May 01, 2013 , missing in , -out-09:10:41:61
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
03,-"lohith-May 03, 2013 , -in-11:39:44:08 , missing out

这是代码。研究它并学习。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class RecordParser {

    private BufferedReader reader;

    private List<Person> people;

    private List<String> output;

    public RecordParser(BufferedReader reader) {
        this.reader = reader;
        this.people = new ArrayList<Person>();
        this.output = new ArrayList<String>();
    }

    public void execute() throws IOException {
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(" , ");
            addPerson(new Person(parts[0]));
            if ((parts[1].contains("-in-")) && (parts[1].contains("-out-"))) {
                String[] inout = parts[1].split("-out-");
                Person person = getPerson(parts[0]);
                person.setInTime(inout[0]);
                person.setOutTime("-out-" + inout[1]);
            } else if (parts[1].contains("-in-")) {
                Person person = getPerson(parts[0]);
                person.setInTime(parts[1]);
            } else {
                Person person = getPerson(parts[0]);
                person.setOutTime(parts[1]);
            }
        }

        // Output the people to the String list
        for (Person p : people) {
            output.add(p.getPerson());
        }
    }

    private void addPerson(Person person) {
        for (Person p : people) {
            if (p.getNameDate().equals(person.getNameDate())) {
                return;
            }
        }
        people.add(person);
    }

    private Person getPerson(String nameDate) {
        for (Person p : people) {
            if (p.getNameDate().equals(nameDate)) {
                return p;
            }
        }
        return null;
    }

    public List<String> getOutput() {
        return output;
    }

    public static void main(String[] args) {
        String input = "02,-\"sangeetha-May 02, 2013 , -in-09:48:06:61\n" +
                "01,-\"lohith-May 01, 2013 , -out-09:10:41:61\n" +
                "02,-\"sushma-May 02, 2013 , -in-09:48:06:61\n" +
                "01,-\"sushma-Jan 01, 2013 , -in-09:07:06:50-out-05:39:01:63\n" +
                "02,-\"sangeetha-May 02, 2013 , -out-08:08:19:27\n" +
                "02,-\"sushma-May 02, 2013 , -out-07:52:13:51\n" +
                "03,-\"lohith-May 03, 2013 , -in-11:39:44:08";

        BufferedReader reader = new BufferedReader(new StringReader(input));
        RecordParser recordParser = new RecordParser(reader);

        try {
            recordParser.execute();

            for (String s : recordParser.getOutput()) {
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 

        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class Person {
        private String nameDate;
        private String inTime;
        private String outTime;

        public Person (String nameDate) {
            this.nameDate = nameDate;
            this.inTime = "missing in";
            this.outTime = "missing out";
        }

        public void setInTime(String inTime) {
            this.inTime = inTime;
        }

        public void setOutTime(String outTime) {
            this.outTime = outTime;
        }

        public String getNameDate() {
            return nameDate;
        }

        public String getPerson() {
            StringBuilder builder = new StringBuilder();
            builder.append(nameDate);
            builder.append(" , ");
            builder.append(inTime);
            builder.append(" , ");
            builder.append(outTime);
            return builder.toString();
        }

    }

}
于 2013-07-11T18:53:26.177 回答