(Java) Read .txt and organize activities for hours / minutes Doubts in logic to generate the output file according to the example
I need that even if he reaches the total_min &lt;720 condition he continues to travel the lines. 720 is the total number of minutes before lunch, when reached, it should separate the activities that should be performed after lunch (lunch must be inserted manually).


I need to make a system that reads a TXT file (intput.txt).
This file has several lines.
Each line an activity with execution time (30min, 45min, 60min)
Activities should start at 9 am.
Half day (12:00) lunch break
Generate a TXT with organized activities (output.txt)



  intput.txt


Correr 60min
Estudar 30min
Ler 45min
Escrever 60min
Caminhar 45min
Jogar 30min



  Example how the output.txt file should look


09:00 Correr 60min
10:00 Estudar 30min
10:30 Ler 45min
11:15 Caminhar 45min
12:00 Almoço 60min
13:00 Jogar
(...)



  My code:


    public static void main(String[] args) throws IOException {
    // TODO code application logic here

    ArrayList&lt;String&gt; antes_almoco = new ArrayList&lt;String&gt;();
    ArrayList&lt;String&gt; depois_almoco = new ArrayList&lt;String&gt;();

    int total_min = 540;
    int horas = total_min / 60;
    int minutos = total_min % 60;
    String trinta = "30min";
    String quarentaCinco = "45min";
    String sessenta = "60min";
    boolean trinta_min = false;
    boolean quarentaCinco_min = false;
    boolean sessenta_min = false;
    String path = "C:\\input.txt";
    String outputDir = "C:\\output.txt";

    FileReader arq = new FileReader(path);
    BufferedReader lerArq = new BufferedReader(arq);
    String linha;

    //Output   
    File file2 = new File(outputDir);
    FileWriter arq_output = new FileWriter(file2, true);
    PrintWriter gravarArq = new PrintWriter(arq_output);

    if (!file2.exists()) {
        file2.createNewFile();
    }

    while ((linha = lerArq.readLine()) != null) {

        trinta_min = linha.toLowerCase().contains(trinta.toLowerCase());
        quarentaCinco_min = linha.toLowerCase().contains(quarentaCinco.toLowerCase());
        sessenta_min = linha.toLowerCase().contains(sessenta.toLowerCase());

        if (sessenta_min == true) {
            total_min += 60;
            if (total_min &lt; 720) {
                antes_almoco.add(linha);
            } else if (total_min &gt; 720) {
                depois_almoco.add(linha);
            }
        }

        if (trinta_min == true &amp;&amp; total_min &lt; 720) {
            total_min += 30;
            if (total_min &lt; 720) {
                antes_almoco.add(linha);
            } else if (total_min &gt; 720) {
                depois_almoco.add(linha);
            }
        }

        if (quarentaCinco_min == true &amp;&amp; total_min &lt; 720) {
            total_min += 45;
            if (total_min &lt; 720) {
                antes_almoco.add(linha);
            } else if (total_min &gt; 720) {
                depois_almoco.add(linha);
            }
        }

        if (total_min == 720) {
            total_min += 60;
            antes_almoco.add("12:00 Almoço");
        }
    }

    for (String cont : antes_almoco) {
        System.out.println(cont);
        gravarArq.printf("%d:%02d %s \n", horas, minutos, cont);
    }
    for (String cont : depois_almoco) {
        //System.out.println(cont);
        gravarArq.printf("%d:%02d %s \n", horas, minutos, cont);
    }
    lerArq.close();
    gravarArq.close();
    arq.close();
}
I revised your code and ran some tests.  

Here is the input to my last test.

Correr 60min
Estudar 30min
Ler 45min
Escrever 60min
Caminhar 45min
Cutting of steel sheets 60min
Jogar 30min


Here is the output from my last test.

09:00 Correr 60min
10:00 Estudar 30min
10:30 Ler 45min
11:15 Escrever 60min
12:15 Almoço 60min
01:15 Caminhar 45min
02:00 Cutting of steel sheets 60min
03:00 Jogar 30min


The first major change I made to your code was to write a method to convert elapsed minutes to time.

I only needed one List to hold the events.  Actually, I didn't need the List at all.  I could have written the string out as I created it, but I left the List in.

I also simplified the processing of each line.  All I need is the number of minutes that the event lasts.  This code can handle any number of minutes, not just 30, 45, or 60.

Edited to add: I revised the code to handle an input line with an event that has spaces in the event text.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Schedule {

    public static void main(String[] args) throws IOException {
        // TODO code application logic here

        List&lt;String&gt; events = new ArrayList&lt;&gt;();

        String path = "C:\\Eclipse\\Eclipse-2020-workspace\\"
                + "com.ggl.testing\\resources\\input.txt";
        String outputDir = "C:\\Eclipse\\Eclipse-2020-workspace\\"
                + "com.ggl.testing\\resources\\output.txt";

        FileReader arq = new FileReader(path);
        BufferedReader lerArq = new BufferedReader(arq);

        // Output
        File file2 = new File(outputDir);
        FileWriter arq_output = new FileWriter(file2, true);
        PrintWriter gravarArq = new PrintWriter(arq_output);

        if (!file2.exists()) {
            file2.createNewFile();
        }

        int totalMinutes = 0;
        int lunchMinutes = 180;
        boolean beforeLunch = true;

        String linha;
        while ((linha = lerArq.readLine()) != null) {
            String[] fields = linha.split(" ");
            int lastIndex = fields.length - 1;
            int endIndex = fields[lastIndex].lastIndexOf("min");
            int duration = Integer.valueOf(
                    fields[lastIndex].substring(0, endIndex));

            String output = "";
            for (int i = 0; i &lt; fields.length; i++) {
                output += fields[i] + " "; 
            }
            output = toTime(totalMinutes) + " " + output;
            events.add(output.trim());

            totalMinutes += duration;

            if (beforeLunch &amp;&amp; totalMinutes &gt;= lunchMinutes) {
                output = toTime(totalMinutes) + " Almoço 60min";
                events.add(output);
                totalMinutes += 60;
                beforeLunch = false;
            }
        }

        for (String cont : events) {
            System.out.println(cont);
            gravarArq.println(cont);
        }

        lerArq.close();
        gravarArq.close();
        arq.close();
    }

    private static String toTime(int minutes) {
        int hours = minutes / 60 + 9;
        hours = hours % 12;
        hours = (hours == 0) ? 12 : hours;
        minutes = minutes % 60;
        return String.format("%02d:%02d", hours, minutes);
    }

}