(Java) Error while continuing to traverse TXT lines

Asked

Viewed 24 times

1

good night!

I need that even if it reaches the condition total_min < 720 He keeps going through the lines. The 720 are the total minutes before lunch, when reached should separates the activities to be performed after lunch.

  • I need to make a system that reads a TXT(intput.txt file).
  • This file has several lines.
  • Each line an activity with run 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 as it should be the output.txt file

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:

  static int i = 0;

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

    ArrayList<String> antes_almoco = new ArrayList<String>();
    ArrayList<String> depois_almoco = new ArrayList<String>();


    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 nomePc = System.getProperty("user.name");
    String path = "\\input.txt";
    String outputDir = "\\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 < 720) {
                total_min += 60;
                antes_almoco.add(linha);
            }

            if (trinta_min == true && total_min < 720) {
                total_min += 30;
                antes_almoco.add(linha);
            }

            if (quarentaCinco_min == true && total_min < 720) {
                total_min += 45;
                antes_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();
}
  • Now the null test on if became unnecessary kkk.

  • Thanks @Piovezan! Removed and worked. Can help me with the rest of the code?

  • Your question is not clear. It looks like this: "Read my code and help me with logic". Be more specific in what you want. Detail better.

  • I need that even if it reaches the condition total_min < 720 He keeps going through the lines. The 720 are the total of minutes before lunch, when reached should separates the activities that are to be performed after lunch. I’m sorry, I’m a beginner and I couldn’t think of another way to do it :(

  • 1

    No problem. Edit the question by putting this detail into it.

1 answer

1


See if it makes sense to you.

Remove the condition total_min < 720 if and in place of antes_almoco.add(linha) place:

if (total_min < 720) {
    antes_almoco.add(linha);
} else if (total_min > 720) {
    depois_almoco.add(linha);
}

I suggest encapsulating this section in a function to reuse the code.

  • 1

    Here we do not revise code in these molds (review a generic logic), but one place you can take your code to evaluate a better way to do is in https://codereview.stackexchange.com

  • Thank you very much! Perfect, I’ll check!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.