Question

I need help understanding this programming assignment. I do not understand it at all. Someone provided me with the code but when I run the code on eclipse it gives an error. Please explain this assignment to me please.

Write a simple line editor. Keep the entire text in an ArrayList object, one line in a separate index position. Start the pro

Class name: FileEdit.java Input file: textin.txt Content of text.txt The first line And another line Output File: textin.txt

Here is the code:

  • 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));

    }

    }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find Below corrected code. This should work. But before you run the code. let me tell you whats going on and whats this assignment is all about. I will make my level best to make you understand the problem.

Lets understand the ques.

We have to create a text editor in which we can edit text. just like vim or emacs or notepad etc

There are 3 main operation that should be present (in notepad there are several and notepad++ many more)

  1. Insert at following line at line number specified. eg  I 3 will insert at line 3
  2. Delete the lien at number specified e.g D 3 will delete line 3
  3. Display the text editor content eg. L

Now lets understand the execution of program

1. Initially when program runs it read the file you provided and print it as is; (students.txt has 3 lines and 2nd line empty)

Input file: students.txt
1> The f lne
2>
3> and another lne
4>

'2. Now the program ask the user what operation do you want to perform on your text. Let's say we want to insert newer lines in our text at line 3. Hence, the line at 3 will shift down

4> I 3
3> second
4> new

3. Now the two new lines have been added at line 3. Now if we want to see how our text now looks like we Display with L

5> L
1> The f lne
2>
3> second
4> new
5> and another lne

4. If you notice "and another lne" is now shifted down as we inserted two lined at 3

5. Now lets say we want to delete line 2 which is empty we give command D 2

5> D 2

6. Now the line at 2 has been deleted.

7. But we cant see it. To do that we command Display L to display our text now.

4> L
1> The f lne
2> second
3> new
4> and another lne

8. We can see empty line gone and all other line has move upward.

9. Next we are done and want to exit we command E

and thats done!!. You can now run the below 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)); } }
Add a comment
Know the answer?
Add Answer to:
I need help understanding this programming assignment. I do not understand it at all. Someone provided...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public...

    I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public class Instruction { public static final int DW = 0x0000; public static final int ADD = 0x0001; public static final int SUB = 0x0002; public static final int LDA = 0x0003; public static final int LDI = 0x0004; public static final int STR = 0x0005; public static final int BRH = 0x0006; public static final int CBR = 0x0007; public static final int HLT...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT