Simple Text Editor

  • + 0 comments

    java 8

    public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner input = new Scanner(System.in);
            int q = input.nextInt();
            
            Stack<String> stack = new Stack<>();
            StringBuilder sb = new StringBuilder();
            
            for(int j = 0; j < q; j++) {
                int choose = input.nextInt();
                switch(choose) {
                    case 1:
                        stack.push(sb.toString());
                        String newText = input.next();
                        sb.append(newText);
                        break;
                    case 2:
                        stack.push(sb.toString());
                        int k = input.nextInt();
                        sb.delete(sb.length() - k, sb.length());
                        break;
                    case 3:
                        int kth = input.nextInt();
                        System.out.println(sb.charAt(kth - 1));
                        break;
                    case 4:
                        sb.setLength(0);
                        sb.append(stack.pop());
                        break;
                }
            }
        }