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)