-4

I am working on a project where we have to create an input file with a poem or a song, and with that file we are supposed to create a program that creates an output file with the input file updated with line numbers. Here is an example: If the input file is: Mary had a little lamb, the output file should look like: /* 1 */ Mary had a little lamb. The code should do this for each line, incrementing the numbers for each line. Below is the code that I have so far, but I am unsure what to do next.

package morrisJCh7Sec2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class PoemTextFile {
    public static void main(String args[]) throws FileNotFoundException {
        File inputFile = new File("poem.txt");
        Scanner input = new Scanner(inputFile);
        PrintWriter outputFile = new PrintWriter("numpoem.txt");

        outputFile.printf("Read in %s. \nWrote out %s.", inputFile, outputFile);
        input.close();
        outputFile.close();
    }
}

This is what my assignment is. I have already created the text file, but when I ran my code the way it is right now, it said that it could not find my file that I had created.

Create a text file with a poem or song lyrics. (If you can’t think of a poem or song, use Mary had a little lamb.) Put one line of the poem or song on one line of the text file. Pay attention to your spelling and punctuation. Use this file as your input file. Write code to read each line from your input file. Write each line to an output file with the line number preceeding the line. Be sure to follow this example for the format of the line number.

Here is the error I receive:

Exception in thread "main" java.io.FileNotFoundException: poem.txt (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.util.Scanner.(Unknown Source) at morrisJCh7Sec2.PoemTextFile.main(PoemTextFile.java:11)

  • 2
    Possible duplicate of [Modify the content of a file using Java](https://stackoverflow.com/questions/13741751/modify-the-content-of-a-file-using-java) – Mohsen Dec 06 '18 at 16:18
  • Which part is giving you trouble? Have you never written a loop before? Are you unfamiliar with the methods of Scanner or the methods of PrintWriter? – VGR Dec 06 '18 at 17:23
  • I have written loops before, but I have never used the PrintWriter method. – Julie Morris Dec 06 '18 at 17:50

1 Answers1

0

You are close. Just iterate over the lines of the text:

    File inputFile = new File("poem.txt");
    Scanner input = new Scanner(inputFile);
    input.useDelimiter("\n");
    PrintWriter outputFile = new PrintWriter("numpoem.txt");
    int count = 0;

    while(input.hasNext()) {
        outputFile.print("/* " + count++ + " */ " + input.next() + "\n");
    }

    input.close();
    outputFile.close();

The delimiter \n (newline) ensures that every new chunk of data read by the Scanner is a line from the text file. The while loop runs until there are no more lines to read. Then you write the line to the target file, adding the line number by using a counting variable which is incremented by the ++ for every line. If you want to start counting at 1, just change the initial value of count.

bkis
  • 2,530
  • 1
  • 17
  • 31