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

Answer the following program in JAVA please: Your program will be a line editor

ID: 3791330 • Letter: A

Question

Answer the following program in JAVA please:

Your program will be a line editor. A line editor is an editor where all operations are performed by entering commands at the command line. Commands include displaying lines, inserting text, editing lines, cutting and pasting text, loading and saving files. For example, a session where the user enters three lines of text and saves them as a new file may appear as:

   Menu: m           Delete line: dl

   Load file: l      Delete range: dr

   Show all: sa      Copy range: cr

   Show line: sl    Paste lines: pl

   Show range: sr   Write to file: w

   New line: nl     Quit: q

   Edit line: el    Write and quit: wq

-> nl

type line? (y/n): y

1: this is the first line

type line? (y/n): y

2: and a second line

type line? (y/n): y

3: and this is the third line

type line? (y/n): n

-> w

write to file: test.txt

-> sa

1: this is the first line

2: and a second line

3: and this is the third line

-> q

The file created is:

this is the first line

and a second line

and this is the third line

The editor will first display a menu after which it will prompt for commands with "-> ". The commands which can be entered can be seen in the menu appearing in the example.

This web page was written with the line editor described here.

The Commands

If you run the program with no command line arguments it will begin with an empty document. If you give the name of an existing file as a command line argument it will open and load the contents of that file. You can exit the program with either the quit, q, or write and quit, wq, command.

Displaying the menu

The menu is displayed when the program starts. It can be displayed with the command m.

Reading and writing files

The load file command l will ask the user for a file name, open the file for reading, read the file contents into the editor and close the file. If there is already text in the editor it is discarded. The write file command w will write the contents of the editor to a file. If a file has previously been opened with the read file command that same file is used for writing. Otherwise, the program will prompt the user for a file name. For either the read or write file commands if the requested file cannot be opened an appropriate error message is displayed.

Displaying text

Text can be displayed with three commands: show all (sa), show range (sr), and show line (sl). All three commands display text with line numbers. Show all displays the entire document. Show range will ask the user a range of lines ("from" and "to") and will display the lines in that range. If the "to" line number is greater than the number of lines in the document lines up to the end of the document will be displayed. Show line will ask the user for a line number and display that line.

Entering new lines of text

New lines of text are entered using the new line command nl. Before entering a new line the program will ask type line? (y/n). If the user chooses yes the line will be accepted after the line number is displayed. If the document is empty the new line will be the first line (line 1) of the document. If the document is not empty the program will prompt the user to enter the line after which the new line will be placed. If the line number is out of range the program will not accept a new line. A new first line can be added by asking to add a line after (the non-existent) line 0. After the user has entered a line the program will continue asking for new lines until the user answers no. As the new lines are being entered they are added sequentially to the document. As the user begins to enter lines at the new line command the line after which the new line is being added is first displayed (unless the user is entering lines at the beginning of the document). Examples:

-> sa

1: this is the first line

2: and a second line

3: and this is the third line

-> nl

insert after line number: 1

inserting after:

this is the first line

type line? (y/n): y

2: one and a half

type line? (y/n): y

3: one and three quarters

type line? (y/n): n

-> sa

1: this is the first line

2: one and a half

3: one and three quarters

4: and a second line

5: and this is the third line

->

The buffers

There are two buffers, the line buffer and the string buffer, used for moving blocks of text. The line buffer holds ranges of entire lines and the string buffer holds substrings of individual lines. Whenever a new range of lines is copied to the line buffer or a string is copied to the string buffer the previous contents of that buffer are deleted (but the other buffer is unaffected).

Moving ranges of lines

Blocks of lines can be moved using copy range, cr, and paste lines, pl. Copy range will ask the user for a range of line numbers ("from" and "to") and will copy the lines to the line buffer - the lines remain unchanged in the document. Paste lines will ask the user to enter the number of the line after which the lines from the line buffer will be pasted. The lines are then copied into the document - the line buffer is left unchanged. Ranges of lines are deleted with delete range, dr. Again, the user is asked for the range of line numbers to delete. The lines are then deleted (the line buffer is unchanged).

Editing a line of text

The edit line command, el will ask for the number of a line to edit then display the line preceded by a scale which allows the user to identify positions in the line by number (beginning with 0). Then the line menu is displayed and the program prompts for input.

-> el

line number: 3

0    5   10   15   20

|----+----|----+----|-

one and three quarters

   Show line: s

   Copy to string buffer: c

   Cut: t

   Paste from string buffer: p

   Enter new substring: e

   Delete substring: d

   Quit line: q

->

The scale is at least as long as the line displayed. Operations chosen from the line menu will then operate on this chosen line until quit line, q.

Show line will display the line (again) with the scale. Quit returns to the main editor menu. The remaining commands edit the chosen line.

Copy to string buffer and Paste from string buffer work like copy range and paste lines but they work with the string buffer rather than the line buffer. Also, Paste from string buffer will insert the substring so that it begins at the specified position. (Thus it does an "insert before" where paste lines does an "insert after.")

-> c

0    5   10   15   20

|----+----|----+----|-

one and three quarters

from position: 4

to position: 7

copied:

0    5   10   15   20

|----+----|----+----|-

one and three quarters

    ^^^^

   Show line: s

   Copy to string buffer: c

   Cut: t

   Paste from string buffer: p

   Enter new substring: e

   Delete substring: d

   Quit line: q

-> p

0    5   10   15   20

|----+----|----+----|-

one and three quarters

insert at position: 14

0    5   10   15   20   25

|----+----|----+----|----+

one and three and quarters

   Show line: s

   Copy to string buffer: c

   Cut: t

   Paste from string buffer: p

   Enter new substring: e

   Delete substring: d

   Quit line: q

->

Delete substring, t, will prompt for a range of characters to delete, will display the string with the substring underlined, ask the user if the range is ok, and then delete the substring.

-> el

line number: 1

0    5   10   15   20

|----+----|----+----|-

this is the first line

   Show line: s

   Copy to string buffer: c

   Cut: t

   Paste from string buffer: p

   Enter new substring: e

   Delete substring: d

   Quit line: q

-> d

0    5   10   15   20

|----+----|----+----|-

this is the first line

from position: 12

to position: 18

delete:

0    5   10   15   20

|----+----|----+----|-

this is the first line

            ^^^^^^^

y/n: y

0    5   10   15

|----+----|----+

this is the ine

Cut, t, combines copy to string buffer and delete substring. It copies the substring to the string buffer and removes it from the line.

Enter new substring, e, will allow the user to type new text into an existing line.

line number: 1

0    5   10   15   20

|----+----|----+----|-

this is the first line

   Show line: s

   Copy to string buffer: c

   Cut: t

   Paste from string buffer: p

   Enter new substring: e

   Delete substring: d

   Quit line: q

-> e

0    5   10   15   20

|----+----|----+----|-

this is the first line

insert at position: 12

text: very very

0    5   10   15   20   25   30

|----+----|----+----|----+----|-

this is the very very first line

Internals

You will define (among others) two classes: a Line class and a Document class. The Line class will hold the text of a line from the document in a field of type String and provide basic operations on a line. Some of these operations are insert a string into the line at a specified position, delete a substring, copy a substring, etc. The Line class will also have a field with the number of characters stored in the line. The Document class will hold an entire document (each line in one String) and provide basic operations such as inserting lines, deleting, copying, and pasting ranges of lines. It will also have a field with the number of lines currently in the document. Elsewhere in your program you will provide code for the operations (chosen by the user from the menu) which will then call the member functions of the Line and Document classes.

The document will be a doubly linked list of lines (Strings) with a dummy node. I.e., if there are 100 lines in the document there will be 101 nodes in the linked list - 100 holding text and one dummy node. This will make insertions and deletions much easier by eliminating special boundary conditions. Line numbers will not be stored with lines. Instead, every time a line number is needed (e.g. for display in show range and show all, finding positions for new line, etc.) it will be found by traversing the list and counting.

All strings will be Java Strings. Lines of text can be manipulated with concatenation and the substring methods:

String substring(int beginIndex) // Returns a new string that is a

                                  // substring of this string.

String substring(int beginIndex, int endIndex) // Returns a new string

                                                // that is a substring of

                                                // this string.

To accept a new line of user input you must use nextLine since reading with next stops accepting input at the first white space and text lines may contain white space. You must use nextLine for all keyboard input: nextLine does not mix well with next, nextInt, etc., so to read integer input you will first read a String and then convert it to an integer using Integer.parseInt.

You will do appropriate input validation. When choosing operations (in response to the "->" prompt) your program will only accept input as stated in the menu. When accepting integer input (line numbers, character positions in a line) your program will check for valid numbers and ranges.

Explanation / Answer

Driver.java

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {

       Document doc = new Document();

       Scanner in = new Scanner(System.in);

       int to, from,lineNum;

       int sent = 1;

       String input;

       while (sent == 1) {

           printDocMenu();

           String res = in.nextLine();

           switch (res) {

           case "m":

               break;

           case "dl":

               System.out.print("Line Number: ");

               int num = Integer.parseInt(in.nextLine());

               doc.removeLine(num);

               break;

           case "l":

               System.out.print(" File Name: ");

               input = in.nextLine();

               doc.loadFile(input);

               break;

           case "dr":

               System.out.print("From: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print("To: ");

               to = Integer.parseInt(in.nextLine());

               doc.removeRange(from, to);

               break;

           case "sa":

               doc.printDoc();

               break;

           case "cr":

               System.out.print("From: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print("To: ");

               to = Integer.parseInt(in.nextLine());

               doc.copyRange(from, to);

               break;

           case "sl":

               System.out.print(" Line Number: ");

               lineNum = Integer.parseInt(in.nextLine());

               doc.showLine(lineNum);

               break;

           case "pl":

               System.out.print("Paste after line: ");

               from = Integer.parseInt(in.nextLine());

               doc.pasteLines(from);

               break;

           case "sr":

               System.out.print(" From: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print(" To: ");

               to = Integer.parseInt(in.nextLine());

               doc.showRange(from, to);

               break;

           case "w":

               System.out.print(" File Name: ");

               doc.writeToFile(in.nextLine());

               break;

           case "nl":

               doc.addLine(in);

               break;

           case "q":

               System.out.println("Quiting");

               sent = 0;

               in.close();

               break;

           case "el":

               System.out.print(" Line Number: ");

               lineNum = Integer.parseInt(in.nextLine());

               doc.editLine(in,lineNum);

               break;

           case "wq":

               System.out.print(" File Name: ");

               input = in.nextLine();

               doc.writeToFile(input);

               System.out.println("Text written to " + input);

               sent = 0;

               in.close();

               break;

           default:

               System.out.println("Please enter a valid option. ");

               break;

           }

       }

   }

   public static void printDocMenu() {

       System.out.printf("Menu: m           Delete line:    dl " + "Load file:   l    Delete range:   dr "

               + "Show all:   sa    Copy range:     cr " + "Show line: sl    Paste lines:    pl "

               + "Show range: sr    Write to file:   w " + "New line:   nl    Quit:            q "

               + "Edit line: el    Write and quit: wq " + "->");

   }

   public static void printLineMenu() {

       System.out.printf(

               " Show line: s " + "Copy to string buffer: c " + "Cut: t " + "Paste from string buffer: p "

                       + "Enter new substring: e " + "Delete substring: d " + "Quit line: q " + "->");

   }

}

Document.java

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.Scanner;

public class Document {

   private Node head;

   private Node bufferHead;

   private int totalLineNum;

   public Document() {

       head = new Node();

       totalLineNum = 0;

   }

   /**

   * addLine takes user input and adds new lines based on the input. Uses the

   * scanner in to retrieve the user input.

   */

   public void addLine(Scanner in) {

       // TODO: TEST IT.

       // Declare variables.

       int sent = 1;

       Node cur = null;

       int afterLineNum = 0;

       // Prompt user for input.

       System.out.print("type line? (y/n): ");

       String res = in.nextLine();

       // Switch based on if the user wants to input a line or not.

       switch (res) {

       case "y":

           // Prompt the user for the line they wish to input after.

           System.out.print("insert after line: ");

           afterLineNum = Integer.parseInt(in.nextLine());

           // If the line to insert after is not 0 then retrieve the node at

           // that position.

           if (afterLineNum != 0) {

               cur = getNode(afterLineNum, head);

               if (cur == null)

                   break;

               System.out.printf("inserting after: %s ", cur.line.getData());

           }

           // While the user still wants to enter lines to the document keep

           // looping over the prompts.

           while (sent == 1) {

               // Prompt user if they wish to enter a new line.

               System.out.print("type line? (y/n): ");

               res = in.nextLine();

               // Switch based on if the user wants to input a line or not.

               switch (res) {

               case "y":

                   // If the line to insert after is 0 then we are adding to

                   // the head of the list.

                   if (afterLineNum == 0) {

                       System.out.printf("%d:", totalLineNum + 1);

                       totalLineNum++;

                       insertTail(in.nextLine(), head);

                   }

                   // Otherwise we are adding after a node.

                   else {

                       System.out.printf("%d:", afterLineNum + 1);

                       afterLineNum++;

                       totalLineNum++;

                       if (cur != null) {

                           insertAfter(cur.line.getData(), in.nextLine(), head);

                           cur = cur.next;

                       }

                   }

                   break;

               case "n":

                   sent = 0;

                   break;

               }

           }

           break;

       case "n":

           sent = 0;

           break;

       default:

           System.out.println("please enter a valid option.");

           break;

       }

   }

   /**

   * removeLine removes a specified line number from the document.

   */

   public void removeLine(int num) {

       if (!isEmpty(head)) {

           if (!(num > totalLineNum)) {

               removeNode(getNode(num, head).line.getData(), head);

               totalLineNum--;

           }

       }

   }

   /**

   * removeRange deletes a range of lines starting from start and ending with

   * end. Start must be less than or equal to end. Start must be less than or

   * equal to the total number of lines in the document.

   *

   */

   public void removeRange(int start, int end) {

       if (start >= end) {

           System.out.println("To must be a less than or equal to From. ");

       } else if (start >= totalLineNum) {

           System.out.printf("Line number: %d does not exist ", start);

       } else {

           if (!isEmpty(head)) {

               if (start == 0)

                   start++;

               if (end > totalLineNum)

                   end = totalLineNum;

               while ((start - 1) != end) {

                   removeLine(end--);

               }

           }

       }

   }

   /**

   * writeToFile iterates over each line in the document and outputs it to the

   * passed filename.

   */

   public boolean writeToFile(String filename) {

       try {

           Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./" + filename), "utf-8"));

           Node currentLine = head.next;

           while (currentLine != null) {

               writer.write(currentLine.line.getData() + " ");

               currentLine = currentLine.next;

           }

           writer.close();

           return true;

       } catch (Exception e) {

           return false;

       }

   }

   /**

* loadFile adds each line in the passed file as a new Line at the end of

   * the document.

   *

   */

   public boolean loadFile(String filename) {

       try {

           File inFile = new File("./" + filename);

           Scanner in = new Scanner(inFile);

           if (totalLineNum != 0) {

               head.next = null;

               totalLineNum = 0;

           }

           while (in.hasNextLine()) {

               totalLineNum++;

               insertTail(in.nextLine(), head);

           }

           in.close();

           return true;

       } catch (Exception e) {

           System.out.println(filename + " does not exist");

           return false;

       }

   }

   /**

   * showLine displays a specified line within the document.

   */

   public void showLine(int lineNum) {

       if (lineNum > totalLineNum) {

           System.out.printf("Line %d does not exist! Total Lines: %d ", lineNum, totalLineNum);

       } else {

           if (lineNum == 0)

               lineNum++;

           Node current = head.next;

           for (int i = 1; i < lineNum; i++) {

               current = current.next;

           }

           System.out.printf("%d: %s ", lineNum, current.line.getData());

       }

   }

   /**

   * showRange displays a specified range of lines within the document.

   */

   public void showRange(int start, int end) {

       if (start > end)

           System.out.println("To must be a larger number than From. ");

       else {

           if (end > totalLineNum)

               end = totalLineNum;

           if (start == 0)

               start++;

           Node current = getNode(start, head);

           if (current != null) {

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

                   System.out.printf("%d: %s ", i, current.line.getData());

                   current = current.next;

               }

           }

       }

   }

   /**

   * copyRange copies the specified range of lines into a document buffer

   */

   public void copyRange(int start, int end) {

       bufferHead = new Node();

       Node current = getNode(start, head);

       while (current != null && start <= end) {

           insertTail(current.line.getData(), bufferHead);

           current = current.next;

           end--;

       }

   }

   /**

   * pasteLines pastes the current document buffer after the specified line.

   */

   public void pasteLines(int start) {

       Node current = getNode(start, head);

       if (current != null) {

           Node currentBuffer = bufferHead.next;

           while (currentBuffer != null && current != null) {

               Node toAdd = new Node(currentBuffer.line.getData());

               toAdd.prev = current;

               toAdd.next = current.next;

               current.next = toAdd;

               if (current.next != null)

                   current.next.prev = toAdd;

               currentBuffer = currentBuffer.next;

               current = current.next;

           }

       }

   }

   /**

   * editLine starts the edit line menu for the specified line.

   */

   public void editLine(Scanner in, int lineNumber) {

       Node current = getNode(lineNumber,head);

       current.line.lineMenu(in);

   }

   /**

   * Displays the entire document to the console.

   */

   public void printDoc() {

       Node current = head.next;

       int count = 1;

       System.out.println();

       while (current != null) {

           System.out.printf("%d:%s ", count, current.line.getData());

           current = current.next;

           count++;

       }

       System.out.println();

   }

   /**

   * toString prints out the string representation of the document.

   */

   @Override

   public String toString() {

       Node current = head.next;

       String toReturn = "";

       int count = 1;

       while (current != null) {

           toReturn += (count + ":" + current.line.getData() + " ");

           current = current.next;

           count++;

       }

       return toReturn;

   }

   /**

   * insertHead adds a new node to the head of the list. This represents

   * adding a new line to the document.

   */

   private void insertHead(String data, Node curHead) {

       Node toAdd = new Node(data);

       if (isEmpty(curHead)) {

           curHead.next = new Node(data);

           toAdd.prev = curHead;

       } else {

           toAdd.next = curHead.next;

           toAdd.prev = curHead;

           curHead.next = toAdd;

       }

   }

   /**

   * insertHead adds a new node to the end of the list. This represents adding

   * a new line to the document.

   */

   private void insertTail(String data, Node curHead) {

       Node toAdd = new Node(data);

       if (isEmpty(curHead)) {

           curHead.next = toAdd;

           toAdd.prev = curHead;

       } else {

           Node current = curHead.next;

           while (current.next != null)

               current = current.next;

           current.next = toAdd;

           toAdd.prev = current;

       }

   }

   /**

   * insertHead adds a new node to after the specified target if it is in the

   * list. If the target value is not in the list then return false else add

   * the data value to the list and return true. This represents adding a new

   * line to the document.

   *

   */

   private boolean insertAfter(String target, String data, Node curHead) {

       if (isEmpty(curHead))

           return false;

       Node current = findNode(target, curHead);

       if (current == null)

           return false;

       Node toAdd = new Node(data);

       toAdd.prev = current;

       toAdd.next = current.next;

       current.next = toAdd;

       return true;

   }

   /**

   * Removes the target value from the list. This represents removing a line

   * from the document.

   */

   private boolean removeNode(String target, Node curHead) {

       Node current = findNode(target, curHead);

       if (current == null)

           return false;

       Node tmp = current.prev;

       if (current.next != null)

           current.next.prev = tmp;

       tmp.next = current.next;

       return true;

   }

   /**

   * isEmpty returns true if the document has no lines in it or false if it

   * does.

   */

   private boolean isEmpty(Node curHead) {

       return curHead.next == null;

   }

   /**

   * Retrieves the node at the position specified by nodeNum.

   *

   */

   private Node getNode(int nodeNum, Node curHead) {

       if (isEmpty(curHead))

           return null;

       if (nodeNum > totalLineNum)

           return null;

       Node cur = curHead.next;

       for (int i = 1; i < nodeNum; i++) {

           cur = cur.next;

       }

       return cur;

   }

   /**

   * Retrieves the first node with the string specified by target.

   */

   private Node findNode(String target, Node curHead) {

       if (isEmpty(curHead))

           return null;

       Node current = curHead.next;

       while (current.next != null && !current.line.getData().equals(target))

           current = current.next;

       if (current.line.getData().equals(target))

           return current;

       else

           return null;

   }

   private class Node {

       Node next, prev;

       Line line;

       public Node() {

           next = null;

           prev = null;

       }

       public Node(String data) {

           next = null;

           prev = null;

           line = new Line(data);

       }

   }

}

Line.java

import java.util.Scanner;

public class Line {

   private String data;

   private StringBuffer buffer = new StringBuffer();

   private StringBuilder tmp = new StringBuilder();

   /**

   * Line is a constructor that creates a new line with a specified data.

   */

   public Line(String data) {

       this.data = data;

   }

   /**

   * getData returns the data currently stored in this line.

   */

   public String getData() {

       return data;

   }

   /**

   * setData sets the data currently stored in this line to the specified

   * data.

   */

   public void setData(String data) {

       this.data = data;

   }

   /**

   * lineMenu handles the edit line menu.

   */

   public void lineMenu(Scanner in) {

       int to, from;

       int sent = 1;

       showLine();

       System.out.println();

       while (sent == 1) {

           printLineMenu();

           String res = in.nextLine();

           switch (res) {

           case "s":

               showLine();

               System.out.println();

               break;

           case "c":

               System.out.print("From: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print("To: ");

               to = Integer.parseInt(in.nextLine());

               copy(from, to);

               break;

           case "t":

               System.out.print("From: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print("To: ");

               to = Integer.parseInt(in.nextLine());

               cut(from, to);

               break;

           case "p":

               System.out.print("Insert at position: ");

               from = Integer.parseInt(in.nextLine());

               paste(from);

               break;

           case "e":

               System.out.print("Insert at position: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print("Text: ");

               String text = in.nextLine();

               enter(from, text);

               break;

           case "d":

               System.out.print("From: ");

               from = Integer.parseInt(in.nextLine());

               System.out.print("To: ");

               to = Integer.parseInt(in.nextLine());

               delete(from, to, in);

               break;

           case "q":

               sent = -1;

               System.out.println();

               break;

           default:

               System.out.println("Please enter a valid option. ");

               break;

           }

       }

       System.out.println();

   }

   /**

   * copy copies the substring starting at index from and ending at index to

   * to the StringBuffer.

   */

   private void copy(int from, int to) {

       tmp = new StringBuilder();

       buffer = new StringBuffer();

       if (!(from > data.length())) {

           if (to > data.length())

               to = data.length();

           buffer.append(data.substring(from, to + 1));

           showCopied(from, to);

           System.out.println();

       } else {

           System.out.println("From must be equal to or less than the length of the line!");

       }

   }

   /**

   * cut copies the substring starting at index from and ending at index to to

   * the StringBuffer and removes it from the String stored in data.

   */

   private void cut(int from, int to) {

       tmp = new StringBuilder();

       buffer = new StringBuffer();

       //TODO: Check for from or to < 0

       if (!(from > data.length())) {

           if (to > data.length())

               to = data.length();

           tmp.append(data);

           buffer.append(tmp.substring(from, to + 1));

           tmp.delete(from, to);

           data = tmp.toString();

           System.out.println();

       } else {

           System.out.println("From must be equal to or less than the length of the line!");

       }

   }

   /**

   * paste pastes the the current String stored in the string buffer into the

   * String stored in data, starting at the index from.

   */

   private void paste(int from) {

       tmp = new StringBuilder();

       if (from > data.length())

           from = data.length();

       tmp.append(data);

       tmp.insert(from, buffer.toString());

       data = tmp.toString();

       System.out.println();

   }

   /**

   * enter inserts the specified text into the String data starting at the

   * index from in the String data.

   */

   private void enter(int from, String text) {

       tmp = new StringBuilder();

       if (from > data.length())

           from = data.length();

       tmp.append(data);

       tmp.insert(from, text);

       data = tmp.toString();

       System.out.println();

   }

   /**

   * delete deletes the substring starting at index from and ending at index

   * to to the StringBuffer.

   */

   private void delete(int from, int to, Scanner in) {

       String input;

       tmp = new StringBuilder();

       if (!(from > data.length())) {

           if (to > data.length())

               to = data.length();

           showCopied(from, to);

           int tmpSent = 1;

           while (tmpSent == 1) {

               System.out.print("y/n: ");

               input = in.nextLine();

               switch (input) {

               case "y":

                   tmp.append(data);

                   tmp.delete(from, to);

                   data = tmp.toString();

                   tmpSent = -1;

                   break;

               case "n":

                   tmpSent = -1;

                   break;

               default:

                   System.out.println("Please enter a valid option");

               }

           }

       } else {

           System.out.println("From must be equal to or less than the length of the line!");

       }

       System.out.println();

   }

   /**

   * showCopied prints out the line and what indices have been copied into

   * the StringBuffer.

   */

   private void showCopied(int from, int to) {

       System.out.println("Copied");

       showLine();

       for (int i = 0; i <= to; i++) {

           if (i >= from)

               System.out.print("^");

           else

               System.out.print(" ");

       }

       System.out.println();

   }

   /**

   * showLine prints the current line with scale.

   */

   private void showLine() {

       for (int i = 0; i < data.length(); i++) {

           if (i % 5 == 0)

               System.out.print(i);

           else

               System.out.print(" ");

       }

       System.out.println();

       for (int i = 0; i < data.length(); i++) {

           if (i % 10 == 0)

               System.out.print("|");

           else if (i % 5 == 0)

               System.out.print("+");

           else

               System.out.print("-");

       }

       System.out.printf(" %s ", data);

   }

   /**

   * printLineMenu prints the line menu.

   */

   private void printLineMenu() {

       System.out.printf(

               "Show line: s " + "Copy to string buffer: c " + "Cut: t " + "Paste from string buffer: p "

                       + "Enter new substring: e " + "Delete substring: d " + "Quit line: q ->");

   }

}