Advertisement
desislava_topuzakova

07. Simple Text Editor

Sep 16th, 2021
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package StackAndQueue;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class demo {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         ArrayDeque <String> wordStates = new ArrayDeque<>();
  12.  
  13.         int n = Integer.parseInt(scanner.nextLine());
  14.         StringBuilder currentText = new StringBuilder();
  15.         for (int count = 1; count <= n; count++) {
  16.             String command = scanner.nextLine(); //{номер} {параметър}
  17.             //"1 [string]" -> ["1", "string"]
  18.             //"2 [count]" -> ["2", "count"]
  19.             //"3 [index]" -> ["3", "index"]
  20.             //"4" -> ["4"]
  21.             String commandNumber = command.split("\\s+")[0]; // "1", "2", "3", "4"
  22.             switch (commandNumber) {
  23.                 case "1":
  24.                     //текст, който трябва да го добавя към съществуващия
  25.                     String textToAdd = command.split("\\s+")[1];
  26.                     currentText.append(textToAdd);
  27.                     wordStates.push(currentText.toString());
  28.                     break;
  29.                 case "2":
  30.                     int countElements = Integer.parseInt(command.split("\\s+")[1]);
  31.                     int startIndex = currentText.length() - countElements;
  32.                     currentText.delete(startIndex, startIndex + countElements);
  33.                     wordStates.push(currentText.toString());
  34.                     break;
  35.                 case "3":
  36.                     int index = Integer.parseInt(command.split("\\s+")[1]);
  37.                     System.out.println(currentText.charAt(index - 1));
  38.                     break;
  39.                 case "4":
  40.                     if(wordStates.size() > 1) {
  41.                         wordStates.pop();
  42.                         currentText = new StringBuilder(wordStates.peek());
  43.                     } else {
  44.                         currentText = new StringBuilder();
  45.                     }
  46.                     break;
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement