首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据文件中写入的内容读取txt文件和调用方法?(撤销堆栈)

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

Stack Overflow用户
提问于 2022-04-15 05:02:56
回答 1查看 35关注 0票数 1

我正在编写撤销堆栈,在读取文件和调用txt文件中使用的方法时遇到了困难。我看过其他帖子,但似乎没有给出明确的答案。我使用algs4库和In类读取文件,使用In类读取文件。问题只有在我进入主要方法之后才会开始。

总体代码

代码语言:javascript
复制
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/

代码语言:javascript
复制
    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);
        }

    }

}

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-04-15 06:21:32

通过以下步骤修复代码:

  1. 您必须初始化您的堆栈,否则您将得到NullPointerException。为堆栈添加一个构造函数,如下所示:

代码语言:javascript
复制
public Undo() {
    undo = new Stack<>();
    redo = new Stack<>();
    reversed = new Stack<>();
}

  1. 修改command方法:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71880301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档