-1

So my problem is that I have a .txt file which contains names, and data that I retrieve from another .txt file which is called "name.txt".
Then I loop on every file name, and for every file I create a .html file which combine a html skeleton and the data I retrieved from my .txt file.
The first .html file is correctly filled, the loop is good too (I have 5 names and it loops 5 times) but only the first .html file is filled, others are empty and I can't figure why.

Here is the txt with the names (staff.txt) :
cberthier
sconnor
jmacclane
agent1
Dorian

One of the name.txt files I create :
Berthier
Corinne
Surveillance entrepôt
pmNd1ldFE7WTk

kit
lampe
lacrymo

And here is my code :

public void ajouterInfosAgent() throws IOException {
        BufferedReader squeletteHtml = new BufferedReader(new FileReader("item-agent.html"));
        BufferedReader staffTxt = new BufferedReader(new FileReader("src/main/Txt/staff.txt"));

        // for each line in staff.txt
        String ligne;
        while ((ligne = staffTxt.readLine()) != null) {
            // substring on the end of the filename to remove the extension
            ligne.substring(ligne.length() - 5);
            // Creation of the html file
            BufferedReader txtFile = new BufferedReader(new FileReader("src/main/Txt/" + ligne + ".txt"));
            File htmlFile = new File("src/main/Html/Generation/Agents/" + ligne + ".html");
            // Creation of a writer to fill the html file
            BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile));
            // first line of the txt is the agent's last name
            String nomAgent = txtFile.readLine();
            // then I take the first name
            String prenomAgent = txtFile.readLine();
            // and then his mission
            String missionAgent = txtFile.readLine();

            // we pass the pwd and blank line
            int i = 0;
            while(i < 2) {
                txtFile.readLine();
                i++;
            }

            // we take the agent's equipment
            StringBuilder equipementAgent = new StringBuilder();
            String equipement;
            while((equipement = txtFile.readLine()) != null) {
                equipementAgent.append("<br><input type=\"checkbox\" name=\"\" checked>\n" + " <label>").append(equipement);
                equipementAgent.append("</label>");
            }

            String ligneSquelette;
            String strRecherche = "id=\"main-container\">";
            // for each line of the HTML skeleton
            while ((ligneSquelette = squeletteHtml.readLine()) != null) {
                writer.write(ligneSquelette);
                // I split the line in words
                String[] words = ligneSquelette.split(" ");
                // for each word
                for (String word : words)
                {
                    // if the word is the same as the string I'm searching for
                    if (word.equals(strRecherche))
                    {
                        // I add the last name, first name, the mission and the agent's equipment
                        writer.write(nomAgent + " " +prenomAgent + " - " + missionAgent + " ");
                        writer.write(String.valueOf(equipementAgent));
                    }
                }
            }
            writer.flush();
            writer.close();
        }
        System.out.println("\nAgent's informations added");
    }
  • Have you run your program under the debugger? You open `squeletteHtml` before the outer `while` loop, its contents are consumed the first time and it reaches end of file, and then it remains at end of file on all subsequent loops. `readLine()` will always return `null`. The inner `while` loop will execute zero times for all further files. – David Conrad Feb 22 '22 at 21:49
  • Thanks for your answers, it was so simple. – Thibault Dassise Feb 23 '22 at 17:08

1 Answers1

0

Thanks to David Conrad I found that I just needed to put the HTML skeleton in my while loop.