-1

I want to read the file and add each entry to an arraylist on a date. But the date should also be included.

File Example:

15.09.2002 Hello, this is the first entry.

\t this line, I also need in the first entry.
\t this line, I also need in the first entry. \t this line, I also need in the first entry.

17.10.2020 And this ist the next entry

I tried this. But the Reader reads only the first Line

public class versuch1 {
public static void main(String[] args) {
    ArrayList<String> liste = new ArrayList<String>();
    String lastLine = "";
    String str_all = "";
    String currLine = "";
    try {
        FileReader fstream = new FileReader("test.txt");
        BufferedReader br = new BufferedReader(fstream);
        while ((currLine = br.readLine()) != null) {
            Pattern p = Pattern
                    .compile("[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]");
            Matcher m = p.matcher(currLine);
            if (m.find() == true) {
                lastLine = currLine;
                liste.add(lastLine);

            } else if (m.find() == false) {
                str_all = currLine + " " + lastLine;
                liste.set((liste.indexOf(currLine)), str_all);

            }

        }
        br.close();

    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());

    }

    System.out.print(liste.get(0) + " "+liste.get(1);
 }
}
Den
  • 11
  • 2
  • 2
    Have you tried anything yet? Where exactly are you stuck? – Mureinik Aug 22 '20 at 08:24
  • You could just split the file text on a date regex like `\d{2}\.\d{2}\.\d{4}`: https://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters – jdaz Aug 22 '20 at 08:31
  • You can match the line that starts with a date, followed by matching all lines that don't start with a date `String regex = "^(?:0[1-9]|[12][0-9]|3[01])[- /.](?:0[1-9]|1[012])[- /.](?:19|20)\\d\\d\\b.*(?:\\R(?!^(?:0[1-9]|[12][0-9]|3[01])[- /.](?:0[1-9]|1[012])[- /.](?:19|20)\\d\\d\\b).*)*";` See https://regex101.com/r/Qog1ag/1 – The fourth bird Aug 22 '20 at 12:32

2 Answers2

1

I have solved my problem :)

public class versuch1 {
public static void main(String[] args) {
    ArrayList<String> liste = new ArrayList<String>();
    String lastLine = "";
    String currLine = "";
    String str_all = "";
    try {
        FileReader fstream = new FileReader("test.txt");
        BufferedReader br = new BufferedReader(fstream);
        currLine = br.readLine();
        while (currLine != null) {
            Pattern p = Pattern
                    .compile("[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2} [0-2]?[0-9]:[0-6]?[0-9]:[0-5]");
            Matcher m = p.matcher(currLine);
            if (m.find() == true) {
                liste.add(currLine);
                lastLine = currLine;

            } else if (m.find() == false) {
                liste.set((liste.size() - 1), (str_all));
                lastLine = str_all;

            }
            currLine = br.readLine();
            str_all = lastLine + currLine;
        }

    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());

    }

    System.out.print(liste.get(1) + " ");
}

}

Den
  • 11
  • 2
0

While reading the lines, keep a "current entry".

If the line read begins with a date, then it belongs to a new entry. In this case add the current entry to the list of entries and create a new current entry consisting of the read line.

If the line did not begin with a date, just add it to the current entry.

For this to work, you need to read the first line into the current entry before the loop. And after the loop you need to add the current entry to the list of entries. This in turn only works if there is at least one line and the first line begins with a date. So handle the special case of no lines specially (use if-else). And report an error if the first line does not begin with a date.

Happy coding.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161