I have an assignment that is supposed to use three stacks to ultimately find palindromes. Two stacks will need to hold the reverse of the input sequence. I need to pop the items from one of those stacks and push them into the third stack. This will obviously create two stacks with reverse items that can be compared to see if the input string is a palindrome.
I have already written a class 'ArrayStack' and 'LinkedStack'. I am now supposed to write a class that has three ArrayStack instance variables, a constructor that takes no parameters and will create three stacks of type ArrayStack and a method that takes a string parameter and returns a boolean. The incoming string will be used to check for Palindromes. The individual characters of the string will need to pushed into one of the stacks.
After that is figured out, I need to do the same thing, but with using the 'LinkedStacked' class.
This is the code I have so far and have tried multiple things and have gotten multiple errors (NPE, etc). Any pointers / explanations / advice would be appreciated. I think I have gotten myself really confused. Thanks in advance for any help offered.
Please note I am a beginner.
public class ArrayPalindrome {
static ArrayStack one;
static ArrayStack two;
static ArrayStack three;
public ArrayPalindrome(){
this.one = one;
this.two = two;
this.three = three;
}
public static boolean isPalindrome (String input ){
String input2 = "";
for (int i = 0; i < input.length(); i++) {
char character = input.charAt(i);
one.push(character);
}
while (!one.isEmpty()) {
// add the character at the top to a string
input2 = input2 + one.pop();
}
return true;
}
public static void main(String[] args) {
if (one.equals(two)){
System.out.println("Palindrome");
}
else {
System.out.println("Not a Palindrome");
}
}