Simple Text Editor

  • + 1 comment

    I am at a loss on this one. Many of the test cases are failing, yet my output when I run locally matches the expected output perfectly. I get 'wrong answer'. If anyone has a clue what it could be let me know. My code:

    private static final Scanner scanner = new Scanner(System.in);
    private static Stack<String> state = new Stack<>();
    
    public static void main(String[] args) throws IOException {
        state.push("");
        while (scanner.hasNext()) {
            String command = scanner.nextLine();
            int op = command.charAt(0) - '0';
            switch (op) {
                case 1:
                    String toAppend = command.substring(2);
                    state.push(append(state.peek(), toAppend));
                    break;
                case 2:
                    int charsToDelete = Integer.parseInt(command.substring(2));
                    state.push(delete(state.peek(), charsToDelete));
                    break;
                case 3:
                    int charToPrint = Integer.parseInt(command.substring(2));
                    System.out.println(state.peek().charAt(charToPrint - 1));
                    break;
                case 4:
                    state.pop();
                    break;
            }
    
        }
    }
    
    private static String append(String text, String toAppend) {
        return text + toAppend;
    }
    
    private static String delete(String text, int charsToDelete) {
        return text.substring(0, text.length() - charsToDelete);
    }