deyanmalinov

08. Simple Text Editor

Jul 26th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Deque;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         int lines = Integer.parseInt(scan.nextLine());
  9.         StringBuilder text = new StringBuilder();
  10.         Deque<StringBuilder> stack = new ArrayDeque();
  11.          while (lines-- > 0){
  12.              String[] line = scan.nextLine().split(" ");
  13.              String comand = line[0];
  14.              switch (comand){
  15.                  case "1":
  16.                      text.append(line[1]);
  17.                      stack.push(new StringBuilder(text));
  18.                      break;
  19.                  case "2":
  20.                      int count = Integer.parseInt(line[1]);
  21.                      int start = text.length() - count;
  22.                      text.delete(start, start + count);
  23.                      stack.push(new StringBuilder(text));
  24.                      break;
  25.                  case "3":
  26.                      System.out.println(text.charAt(Integer.parseInt(line[1])-1));
  27.                      break;
  28.                  case "4":
  29.                      if (stack.size() > 1){
  30.                          stack.pop();
  31.                          text = stack.peek();
  32.                      }else if (stack.size() == 1){
  33.                              text = new StringBuilder();
  34.                          }
  35.                      break;
  36.              }
  37.          }
  38.     }
  39. }
Add Comment
Please, Sign In to add comment