如何读取 txt 文件并根据文件中写入的内容调用方法?(撤消堆栈)



我正在写一个撤销堆栈,我在读取文件和调用在txt文件中编写的方法时遇到了麻烦。我看了其他的帖子,但他们似乎没有提供一个明确的答案。我使用的是algs4库和In类来读取文件和In类来读取文件。只有在main方法中才会出现问题。

整体代码

package edu.princeton.cs.algs4;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
 *
 *
 *
 * @author James Bond
 */
public class Undo {
    private Stack<Character> undo;
    private Stack<Character> redo;
    private Stack<Character> reversed;
    /**
     *
     * @param write: This method uses while loop to empty all the elements from
     * the stack.
     * @param write: It also uses the push and pop method to put and remove
     * items from the stack.
     * @param ch: The character variable ch stores input from the user.
     */
    public void write(Character ch) {
        undo.push(ch);
        while (redo.isEmpty() != true) {
            redo.pop();
        }
    }
    public void Undo() {
        if (undo.isEmpty()) {
            throw new NoSuchElementException("Stack Underflow");
        }
        char poppedUndoChar = undo.pop();
        redo.push(poppedUndoChar);
    }
    /**
     * @param redo: Uses and if statement to check if the Stack is empty. and
     * throws and exeception if it is empty. It also pushes the popped redo item
     * onto thee the undo stack.
     */
    public void redo() {
        if (redo.isEmpty()) {
            throw new NoSuchElementException("Stack Underflow");
        }
        char poppedRedoChar = redo.pop();
        undo.push(poppedRedoChar);
    }
    private void query(String str) {
        int n = str.length();
        if (str.equals("undo")) {
            Undo();
        } else if ("redo".equals(str)) {
            redo();
        } else if ("write".equals(str)) {
            write(str.charAt(n - 1));
        } else if ("read".equals(str)) {
            read();
        } else if ("clear".equals(str)) {
            clear();
        }
    }
    public void read() {
        while (undo.isEmpty() == false) {
            reversed.push(undo.pop());
        }
        while (reversed.isEmpty() == false) {
            System.out.println(reversed.pop());
        }
    }
    public void clear() {
        while (undo.isEmpty() == false) {
            undo.pop();
        }
        while (redo.isEmpty() == false) {
            redo.pop();
        }
    }
    public void command(String str) {
        if ((str instanceof String) == false) {
            throw new IllegalArgumentException("Incorrect Input");
        }
        int n = str.length();
        if (str.equals("write")) {
            write(str.charAt(6));
        } else if (str.equals("undo")) {
            Undo();
        } else if (str.equals("redo")) {
            redo();
        } else if (str.equals("clear")) {
            clear();
        } else if (str.equals("read")) {
            read();
        }
    }
    // Scanner input = new Scanner(str);
    // Ssting out = input.nextLine();
    // System.out.print(out);
    //
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        In input = new In("C:\Users\James Bond\input.txt");
        String str = input.readAll();
        Undo kgb = new Undo();
        System.out.println(str);
        
        while(input.readLine() != null){
            kgb.command(str);
            System.out.println(str);
        }
    }
}

惹事生非的人:具体来说,我需要读取文件并调用方法。

文件中的文本如下:

写c

写r

写t

撤销

撤销

写t

明确

应该产生以下输出:

问题的根源是我的主要方法。

更多内容请访问:https://www.geeksforgeeks.org/implement-undo-and-redo-features-of-a-text-editor/

    public static void main(String[] args) {
        In input = new In("C:\Users\James Bond\input.txt");
        String str = input.readAll();
        Undo kgb = new Undo();
        System.out.println(str);
        
        while(input.readLine() != null){
            kgb.command(str);
            System.out.println(str);
        }
    }
}

任何帮助都将是非常感激的。

按以下步骤修复代码:

  1. 你必须初始化你的堆栈,否则你会得到NullPointerException。为你的堆栈添加如下构造函数:
public Undo() {
    undo = new Stack<>();
    redo = new Stack<>();
    reversed = new Stack<>();
}
  1. 修改command方法:

str.equals("write")替换为str.startsWith("write")

最新更新