简体   繁体   English

从文本文件中读取数据,比较行并以首选格式显示

[英]Reading data from a text file, comparing lines & displaying in preferred format

I am reading a text file which contains the log in and out values of an employees, and trying to display in single line bot in and out values which are in different line for same employee and date.the data in that text file is as shown below. 我正在读取一个包含员工的登录和注销值的文本文件,并尝试在同一行和同一雇员的不同行中以单行bot的输入和输出值显示。该文本文件中的数据如下所示下面。

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

example: line 1 and line5 are in and out values for same employee sangeetha, so it should display like: 示例:第1行和第5行是同一员工sangeetha的输入和输出值,因此应显示为:

02,-"sangeetha-May 02, 2013 , -in-09:48:06:61, -out-08:08:19:27

Perhaps I am getting this output, but line 2, it don't have in value, so my code is not able to displaying that only which ever employee having both in and out values in different like its displaying those. 也许我得到了这个输出,但是第2行却没有值,因此我的代码无法仅以显示其值的方式显示那位员工同时拥有不同价值的输入和输出。 I want to display theses records also with missing message appended to it..my code is this.. 我也想显示这些记录,并在上面附加缺少的消息。我的代码是这样。

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(","));
  }
}

Please can anybody modify it to show records which are having only either in entry or out entry? 请有人可以对其进行修改以显示仅具有输入项或输出项的记录吗?

For example..line 2 should display like: 例如..第2行应显示为:

line 2. 01,-"lohith-May 01, 2013 ,missing in, -out-09:10:41:61..

Presently I am getting output is: 目前我得到的输出是:

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

I want that line 2 record to display with these. 我希望第2行记录与这些一起显示。

The biggest problem with your code is that it is one monolithic mess. 您的代码最大的问题是,它是一个单一的混乱。 It's hard to see where you're going wrong. 很难看到你要去哪里错了。

I put together some code to process your input. 我整理了一些代码来处理您的输入。 The reason I wrote this code is to show you and the others reading this later how to break monolithic code into tiny, testable pieces. 我编写此代码的原因是向您和其他人稍后展示如何将整体代码分解为可测试的小片段。

Not including the input string I created (since you have an input file), there is no method in this code longer than 20 lines. 不包括我创建的输入字符串(因为有输入文件),因此该代码中没有方法超过20行。 Some are much shorter. 有些要短得多。

I ran this code three times. 我运行了这段代码三遍。 The first time, I forgot to code the output method. 第一次,我忘记对输出方法进行编码。 The second time, I had to correct the output format. 第二次,我不得不更正输出格式。 The third time, I produced this output. 第三次,我产生了这个输出。

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

And here's the code. 这是代码。 Study it and learn. 学习并学习。

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();
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM