Advertisement
nikeza

08. Simple Text Editor

Sep 22nd, 2019
121
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.Scanner;
  3.  
  4. public class stack1_Exercises_8Simple_Text_Editor {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.  
  10.         ArrayDeque<String> operation = new ArrayDeque<>();
  11.         String text = "";
  12.         for (int i = 0; i < n; i++) {
  13.             String[] tokens = scanner.nextLine().split(" ");
  14.             int command = Integer.parseInt(tokens[0]);
  15.  
  16.             switch (command) {
  17.                 case 1:
  18.                     operation.push(text);
  19.                     text = text + tokens[1];
  20.  
  21.                     break;
  22.                 case 2:
  23.                     operation.push(text);
  24.  
  25.                     int del = Integer.parseInt(tokens[1]);
  26.                     for (int j = 0; j < del; j++) {
  27.                         text = text.substring(0, text.length() - 1);
  28.                     }
  29.  
  30.                     break;
  31.                 case 3:
  32.  
  33.                     int count = Integer.parseInt(tokens[1]) - 1;
  34.  
  35.                     try {
  36.                         System.out.println(text.charAt(count));
  37.                     } catch (Exception e) {
  38.                         e.getMessage();
  39.                     }
  40.  
  41.                     break;
  42.                 case 4:
  43.  
  44.                     text = operation.pop();
  45.  
  46.                     break;
  47.             }
  48.         }
  49.  
  50.      
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement