I have this code where I'm trying to implement the isPalindrome method using Stack but I get a "cannot be resolved to a type". What to understand from that and how to fix it?
Here is my output
[>>] Enter a string (or a ! to exit): csc Exception in thread "main" java.lang.Error: Unresolved compilation problems: LinkedStack cannot be resolved to a type The method push(char) is undefined for the type PalindromeChecker // also here, can you explain me this error? I thought that it was possible to push character into a stack at assignment03PartB.PalindromeChecker.isPalindrome(PalindromeChecker.java:30) at assignment03PartB.PalindromeChecker.main(PalindromeChecker.java:55)
Driver
import java.util.Scanner; // // - Do not change the "main" method. // - Please ADD CODE to complete implementing the program // public class PalindromeChecker { private static boolean isPalindrome(String string) { char[] ch = string.toCharArray(); for (char c : ch) { Sy stem.out.println(c); } StackInterface<Character> myStack = new LinkedStack<>(); // error come on this line System.out.println("isEmpty() returns " + myStack.isEmpty()); System.out.println("ch size : " + ch.length); for (int i = 0; i < ch.length; i++) { push(ch[i]); } System.out.println("Job done."); return true; } // // - Do not change the "main" method. // - Please ADD CODE to complete implementing the program // public static void main(String[] args) { // // - Do not change the "main" method. // - Please ADD CODE to complete implementing the program // Scanner input = new Scanner(System.in); System.out.print("[>>] Enter a string (or a ! to exit): "); String string = input.nextLine(); while (!string.equals("!")) { if (isPalindrome(string)) { System.out.println(" [+] Yes. \"" + string + "\" IS a palindrome!"); } else { System.out.println(" [-] No. \"" + string + "\" is NOT a palindrome!"); } System.out.print("[>>] Enter a string: "); string = input.nextLine(); } System.out.println("[<<] Thank you!"); // // - Do not change the "main" method. // - Please ADD CODE to complete implementing the program // } }
Stack Interface
public interface StackInterface<T> { public void push(T newEntry); public T pop(); public T peek(); public boolean isEmpty(); public void clear(); }
OurStack class implementing StackInterface
public class OurStack<T> implements StackInterface<T> { public OurStack() { topNode = null; } @Override public void push(T newEntry) { topNode = new Node(newEntry, topNode); } @Override public T peek() { if (isEmpty()) { throw new EmptyStackException(); } else { return topNode.getData(); } } @Override public T pop() { T top = peek(); // Might throw EmptyStackException assert (topNode != null); topNode = topNode.getNextNode(); return top; } @Override public boolean isEmpty() { return topNode == null; } @Override public void clear() { topNode = null; } }