Question: Line oriented editor in java My line editor program doesnot erase the line as shown in the output like If the user tries to go

Line oriented editor in java

My line editor program doesnot erase the line as shown in the output like If the user tries to go past either end of the file, the command is ignored. If the user tries to remove or change line 0, a "not applicable" message gets printed, and the command has no other effect. (Do not prompt for or read a new line value when the command is not applicable.). When the command 'x' is given it should remove the lines from bottom and if the pointer is 0 it should diaplay not applicable here.

My code:

import java.util.*;

public class LineEditor {

public static void main(String[] args) {

List l = new ArrayList(20); //List of strings: File size 20 String ch = "", e = ""; // ch = for commands e = for new lines

int ptr = 0, ctr = 0; // counter and pointer Scanner sc = new Scanner(System.in); printIntroduction();

while (true) { System.out.print("At line " + ptr + " of " + ctr + " >>> "); ch = sc.nextLine(); if (ch.equalsIgnoreCase("q")) { System.out.println(" Goodbye!"); break; } else { int l1 = ch.length(); for (int i = 0; i < l1; i++) { char c = ch.charAt(i); if (c == '+' && ptr < ctr && ctr > 0) { ptr++; } else if (c == '-' && ptr > 0 && ctr > 0) { ptr--; } else if (c == 'a' || c == 'A') { System.out.print("New line: "); e = sc.nextLine(); l.add(ptr, e); ptr++; ctr++; } else if (c == 'x' || c == 'X') if (ptr > 0 && ctr > 0){ if (ptr == 0) { System.out.println("---not applicable at this time---"); } else { l.remove(ptr - 1); ptr--;

}

ctr--;

} else {

ptr++; } } if (ch.isEmpty()) { ptr++; } System.out.println(" ---[[[START]]]---"); for (int i = 0; i < ctr; i++) { System.out.println((i + 1) + ": " + l.get(i)); } System.out.println("----[[[END]]]---- "); } }

}

private static void printIntroduction() { System.out.println("Line Oriented Editor -------------------- This program lets you create and edit a file line by line. Enter ? for help. NOTE: There is no save function!"); }

}

OUTPUT should look like this

---[[[START]]]--- 1: New first line. 2: New second line. 3: This is my NEW new third line. 4: (Old) new third line... 5: ...fourth... 6: ...and fifth lines. ----[[[END]]]----

At line 4 of 6 >>> xxx ---[[[START]]]--- 1: New first line. 2: New second line. 3: This is my NEW new third line. ----[[[END]]]---- At line 3 of 3 >>> -- ---[[[START]]]--- 1: New first line. 2: New second line. 3: This is my NEW new third line. ----[[[END]]]---- At line 1 of 3 >>> - ---[[[START]]]--- 1: New first line. 2: New second line. 3: This is my NEW new third line. ----[[[END]]]---- At line 0 of 3 >>> x ---not applicable at this time--- ---[[[START]]]--- 1: New first line. 2: New second line. 3: This is my NEW new third line. ----[[[END]]]----

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!