Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class stack1_Exercises_8Simple_Text_Editor {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- ArrayDeque<String> operation = new ArrayDeque<>();
- String text = "";
- for (int i = 0; i < n; i++) {
- String[] tokens = scanner.nextLine().split(" ");
- int command = Integer.parseInt(tokens[0]);
- switch (command) {
- case 1:
- operation.push(text);
- text = text + tokens[1];
- break;
- case 2:
- operation.push(text);
- int del = Integer.parseInt(tokens[1]);
- for (int j = 0; j < del; j++) {
- text = text.substring(0, text.length() - 1);
- }
- break;
- case 3:
- int count = Integer.parseInt(tokens[1]) - 1;
- try {
- System.out.println(text.charAt(count));
- } catch (Exception e) {
- e.getMessage();
- }
- break;
- case 4:
- text = operation.pop();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement