Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a program written in Java. Write a simple line editor. Keep the entire te

ID: 3865042 • Letter: I

Question

I need a program written in Java.

Write a simple line editor. Keep the entire text in an ArrayList object, one line in a separate index position. Start the program with entering EDIT file, after which a prompt appears along with the line number. If the letter I is entered with a number n following it, then insert the text to be followed on line n. If I is not followed by a number, then insert the text on the current line. If D is entered with two numbers n and m,one n, or no number then delete lines n through m, line n, or the current line. Do the same with commandL, which stands for listing lines. If A is entered, then append the text to the existing lines. Entry E signifies exit and saving a text file.

Example:

Input file: textin.txt

The first line

And another line

EDIT testin

1>The first line

2>

3> And another line

4> I 3

3> The second line

4> One more line

5> L

1>The first line

2>

3> The second line

4> One more line

5> And another line     // This is now line 5, not 3

5> D 2                          // line 5, since L was issued form line 5

4> L                             // line 4, since one line was deleted

1>The first line

2> The second line

3> One more line

4> And another line

5> E

Output File: textin.txt

The first line

The second line

One more line

And another line

Explanation / Answer

/* LineEditor.java */

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class LineEditor {
   public static void main(String[] args) {
       try{
           Scanner sc = new Scanner(System.in);
           String file ="textin.txt";
           FileWriter fw = new FileWriter("textin.txt");
           BufferedWriter bw = new BufferedWriter(fw);
           ArrayList<String> al = new ArrayList<String>();
           System.out.println("Input file: "+file);
           System.out.println("The first line And another line");
           System.out.println("EDIT "+file);
           char ch = '';
           int i=1;
           do{
               System.out.print(i+">");
               String line = sc.nextLine();
               if(line.length()==0){
                   i++;
                   al.add(line);
                   continue;
               }
               else{
               ch = line.charAt(0);
               if(ch=='I'){
                   if(line.charAt(2)!=''){
                       int index = Character.getNumericValue(line.charAt(2));
                       i = index;
                       System.out.print(i+">");
                       line = sc.nextLine();
                       al.add(index-1,line);
                   }
                   else
                       al.add(line);
                   i++;
               }
               else if(ch=='D'){
                   if(line.charAt(2)!=''){
                       int index = Character.getNumericValue(line.charAt(2));
                       al.remove(index-1);
                   }
                   else
                       al.remove(i-1);
                   i--;
               }
               else if(ch=='L'){
                       for(String s:al)
                           System.out.println(s);
               }
               else if(ch=='E'){
                   break;
               }
               else{
                   al.add(line);
                   i++;
               }
               }
           }while(ch!= 'E');
           System.out.println("OutputFile: "+file);
           for(String s:al){
               bw.write(s);
               System.out.println(s);
               bw.newLine();
           }
           bw.close();
           fw.close();
       }
       catch(Exception e){
           e.printStackTrace();
       }
   }
}


/* Sample Input and Output */

Input file: textin.txt
The first line
And another line
EDIT textin.txt
1>The first line
2>
3>Add another line
4>I 3
3>The second line
4>One more line
5>Add another line
6>D 2
5>L
The first line
The second line
Add another line
One more line
Add another line
5>E
OutputFile: textin.txt
The first line
The second line
Add another line
One more line
Add another line

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote