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

JAVA Write a simple line editor. Keep the entire text in an ArrayList object, on

ID: 3734179 • Letter: J

Question

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. IfI 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 following it, then delete lines n through m, line n, or the current line. Do the same with the command L, which stands for listing lines. If A is entered, then append the text to the existing lines. Entry E signifies exit and saving the text in a file Here is an example: EDIT testfile 1> The first line 3> And another line 4> I 3 3>The second line > One more line 5> L 1> The first line 2> 3> The second line > One more line 5> And another line /This is now line 5, not 3; 5> D 2 4> L 1> The first line 2> The second line 3> One more line 4> And another line 5> E // line 5, since L was issued from line 5; // line 4, since one line was deleted; //this and the following line was deleted: // now have new number;

Explanation / Answer

PROGRAM:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class TextEditor {

   public static List<String> lines = new ArrayList<String>();

   public static void main(String[] args) throws IOException

   {

       Scanner s = new Scanner(System.in);

       System.out.print("Input file: ");

       String fileName = s.nextLine();

       BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));

       String str="";

       int count=0;

       while((str=br.readLine())!=null)

       {

           lines.add(count, str);

           System.out.println((++count) + "> "+ str);

       }

       String text="";

       String command = "";

       System.out.print((++count)+"> ");

       command=s.nextLine();

       String commandArr[];

       int flag;

       while(!command.equals("E"))

       {

           flag=0;

           commandArr = command.split("\s");

           if(commandArr[0].equals("I"))           //Insertion

           {

               while(true)

               {

                   if(commandArr.length==1&&flag!=2)

                   {

                       System.out.print((count)+"> ");

                       text = s.nextLine();

                       insertLine(text,count-1);

                   }

                   else if(commandArr.length==2&&flag!=2)

                   {

                       System.out.print((commandArr[1])+"> ");

                       count=Integer.parseInt(commandArr[1]);

                       text = s.nextLine();

                       insertLine(text,Integer.parseInt(commandArr[1])-1);

                   }

                   Else

                       insertLine(text,count-1);

                   System.out.print((++count)+"> ");

                   command=s.nextLine();

                   commandArr = command.split("\s");

                   if(commandArr[0].equals("I")||commandArr[0].equals("L")||commandArr[0].equals("D")||commandArr[0].equals("E"))

                   {

                       flag=1;

                       break;

                   }

                   else

                   {

                       flag=2;

                       text=command;

                   }

               }

           }

           else if(commandArr[0].equals("L"))           //Listing

           {

               printList();

           }

           else if(commandArr[0].equals("D"))           //Deletion

           {

               if(commandArr.length==1)

               {

                   deleteLine(count-1,count-1);

               }

               else if(commandArr.length==2)

               {

                   deleteLine(Integer.parseInt(commandArr[1])-1,Integer.parseInt(commandArr[1])-1);

               }

               else if(commandArr.length==3)

               {

                   deleteLine(Integer.parseInt(commandArr[1])-1,Integer.parseInt(commandArr[2])-1);

               }

               count=lines.size();

           }

           if(flag!=1)

           {

               System.out.print((count++)+"> ");

               command = s.nextLine();

           }

       }

   }

   public static void insertLine(String text,int count)

   {

       lines.add(count,text);

   }

   public static void deleteLine(int start,int end)

   {

       for(int i=start;i<=end;i++)

           lines.remove(i);

   }

   public static void printList()

   {

       for(int i=0;i<lines.size();i++)

           System.out.println((i+1)+"> "+lines.get(i));

   }

}