-2

I have a text file with 5 lines, I wish to read in those lines and be able to number them 1 - 5, and save them in a different file. The numbers begin before the start of the line. I have tried to hard code in a loop to read in the number but I keep getting errors.

public class TemplateLab5Bronze {

    static final String INPUT_FILE = "testLab5Bronze.txt";
    static final String OUTPUT_FILE = "outputLab5Bronze.txt";

    public static void main(String[] args) {
        try {
            FileReader in = new FileReader(INPUT_FILE);
            PrintWriter out = new PrintWriter(OUTPUT_FILE);
            System.out.println("Working");

            BufferedReader inFile = new BufferedReader(in);
            PrintWriter outFile = new PrintWriter(out);
            outFile.print("Does this print?\n");

            String trial = "Tatot";
            outFile.println(trial);
            System.out.format("%d. This is the top line\n", (int) 1.);
            System.out.format("%d.                     \n", (int) 2.);
            System.out.format("%d. The previous one is blank.\n", (int) 3.);
            System.out.format("%d. Short one\n", (int) 4.);
            System.out.format("%d. This is the last one.\n", (int) 5.);
            /*if(int j = 1; j < 6; j++){
                outFile.print( i + trial);
            }*/

            String line;
            do {
                line = inFile.readLine();
                if (line != null) {
                }

            } while (line != null);
            inFile.close();

            in.close();
            outFile.close();
        } catch (IOException e) {
            System.out.println("Doesnt Work");
        }
        System.out.print("Done stuff!");
    }
}

This is all the code I have so far, excluding the import statements, the commented for loop is what I was trying to use. Is there another way to do this?

baudsp
  • 4,076
  • 1
  • 17
  • 35
Ted.M
  • 1
  • 6
  • 1
    That commented `loop` is, in fact, no loop, but a conditional. I think you were thinking about `for`loop (which has same syntax as written above). – Shirkam Oct 23 '17 at 15:37
  • @Berger its printing all the lines 5 times and labeling them 1 - 5 – Ted.M Oct 23 '17 at 18:49

2 Answers2

0

You don't need two PrintWriters. Use only one.

PrintWriter outFile = new PrintWriter(OUTPUT_FILE);

You can simply use a counter instead of a for loop (which you have incorrectly written as if - as mentioned by @Shirkam)

    String line;
    int count=1;
        do {
            line = inFile.readLine();
            if (line != null) {
                outFile.println( count++ +"." + line);
            }

        } while (line != null);
        inFile.close();

This works fine at my end.

phoenixSid
  • 447
  • 1
  • 8
  • 22
  • Since im using a do, do i have to throw the exceptions? – Ted.M Oct 23 '17 at 19:01
  • @Ted.M To throw exceptions or to use a try catch is a separate discussion. You should google and read up on it. Here’s something to start with- [https://stackoverflow.com/questions/33137563/when-should-we-throw-exceptions-or-catch-exceptions-in-a-method] – phoenixSid Oct 23 '17 at 19:37
0

One way to do it is to add to the printWriter while looping through the existing file:

FileReader fr = new FileReader("//your//path//to//lines.txt");
        BufferedReader br = new BufferedReader(fr);
        try (PrintWriter writer = new PrintWriter("//your//other//path//newlines.txt", "UTF-8")) {
            String line;
            int num = 1;
            while ((line = br.readLine()) != null) {
                writer.println(num + ". " + line);
                num++;
            }
        }

Note: I didn't put in any catch statements, but you might want to catch some/all of the following: FileNotFoundException, UnsupportedEncodingException, IOException

achAmháin
  • 4,176
  • 4
  • 17
  • 40