I have the following program in Java import java.io.BufferedReader; import java.
ID: 3802049 • Letter: I
Question
I have the following program in Java
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 FileEdit{
public static List lines = new ArrayList();
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
System.out.println((i+1)+"> "+lines.get(i));
}
}
I need to add an Append commande to the file editor by keying in A. This will allow the user to add more to the current line of the array list.
I also need to save the changed file to nex .txt file called textout.txt
The output will just be lines of text that have not been edited out for example:
The first line
The second line
One more line
And another line
Here is the original assignment so you can better understand.
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 command L, which stands for listing lines. If A is entered, then append the text to the existing lines or the last line. 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>D 2 4
4>L
1>The first line
2>A
1> not done yet
2> L
1>The first line not done yet
2> E
Output File: textout.txt
The first line not done yet // the line that are left or changed after entering E must be saved in new text file. textout.txt
Explanation / Answer
Please find Solution below to your queries :
I need to add an Append commande to the file editor by keying in A.
public static void insertLine(String text)
{
lines.apend(text); // apend is always at end of file/ String
}
I also need to save the changed file to nex .txt file called textout.txt
//Import File Writer
import java.io.FileWriter;
public static void save() // or you can input file name with which it is to be saved too.
{
FileWriter writer = new FileWriter("textout.txt");
for(String str: arr) { writer.write(str); }
writer.close();
}