从文本文件中读取数据,比较线条和数据。以首选格式显示

时间:2013-07-11 14:06:05

标签: java string parsing file-io

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

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行记录。

1 个答案:

答案 0 :(得分: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();
        }

    }

}