Question: -------------------------------------------------------------------------------------------------------------------------------------- I am trying to create a java method that has two file inputs. The method reads the first file then writes it to another

--------------------------------------------------------------------------------------------------------------------------------------

I am trying to create a java method that has two file inputs. The method reads the first file then writes it to another file. However, while writting to the second file the method should add line numbers as shown bellow. I believe that my code is reading and writing properly, however, I do not know how to do the number formating "The line number should be right aligned in three spaces, be followed by a space, then a pipe, another space, and lastly the original line."

This is the text given to me for the assignment.

Create a class named LineNumbers and implement the following method in it. public static void process(File input, File output)

This method receives a text file. The method writes all lines from the input file to the output file with each line preceded by its line number formatted as follows:

### | Original Line 
 

The line number should be right aligned in three spaces, be followed by a space, then a pipe, another space, and lastly the original line. For example:

Input File:

Output File:

 This is a line of text 
 Yet another line 
 You only live twice 

.

 This is the last line 
 1 | This is a line of text 
 2 | Yet another line 
 3 | You only live twice 

.

123 | This is the last line 

You may assume that no file will have more than 999 lines.

Here is My code

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class LineNumbers {

public static void process(File input, File output){

//read in a file(input)

Scanner iRead;

try{

iRead = new Scanner(input);

}

catch (FileNotFoundException e){

//if the file doesnt exist return

System.out.println("I can't find " + input);

return;

}

ArrayList lines = new ArrayList();

//read all of the lines in the input file into arraylist

int i = 1;

while(iRead.hasNextLine()){

lines.add(iRead.nextLine());

}

// done reading, close scanner

iRead.close();

PrintWriter writer;

try{

writer = new PrintWriter(output);

}

catch(FileNotFoundException e){

System.out.println("Can't find " + output);

return;

}

//Write to new file

for(String line : lines){

String a = line;

// I'm guessing this is where I would need to do the formating for the number?

a = i + " | " + a;

writer.println(a);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!