Reputation: 11
I'm writing an Undo Stack and I'm having trouble reading the file and invoking the methods written with in the txt file. I've looked at other post but they don't seem to provide a clear answer. I'm using the algs4 library and the In class to read the file and the In class to read the file. Problems only start once I'm in the main method.
Overall Code
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);
}
}
}
Trouble Maker: Specifically I need to read the file and invoke the methods.
The text in the file is as follows:
write c
write a
write r
write t
undo
undo
write t
read
Which should produce this output:
cat
The source of the problem is my main method.
More context at: 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);
}
}
}
Any help would be greatly appreciated.
Upvotes: 1
Views: 66
Reputation: 1706
Fix your code by following steps:
public Undo() {
undo = new Stack<>();
redo = new Stack<>();
reversed = new Stack<>();
}
command
method:replace str.equals("write")
to str.startsWith("write")
Upvotes: 1