-4

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");
        }
    }
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • you should have the stack trace of the information but for sure the constructor is wrong this.one = one; you are assigning one to one, what you want to do is this.one=new ArrayStack(); (the same for two and three) – vmrvictor Sep 28 '18 at 11:29
  • 3
    This is too broad. So let's just start with the first obvious problem (besides the broken indentation). What do you think `this.one = one` does? Hint: how could a ArrayStack exist if you never have `new ArrayStack()` anywhere in the code? Also, stop putting static everywhere. Read the tutorial about static to understand what that is about. https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – JB Nizet Sep 28 '18 at 11:31
  • Why 3 stacks when it's easy to check or palindrome using one stack? – Kaushal28 Sep 28 '18 at 11:33
  • thanks. @JB Nizet have changed to create the new ArrayStack. if i don't use static i get an error saying i'm access non-static variables. I'm still getting the NPE even with the change of = new ArrayStack(); – student2018 Sep 28 '18 at 12:19
  • @Kaushal28 i'm unsure why three stacks. it makes more difficult - - but i'm assuming it's get practice with using stacks and the logic – student2018 Sep 28 '18 at 12:22
  • @vmrvictor i have updated but I am still getting an NPE – student2018 Sep 28 '18 at 12:22
  • @student2018 check the StackTrace (the error in the console) you will see in which line the problem is. – vmrvictor Sep 28 '18 at 12:32
  • 1
    @vmrvictor it's line 40 - - if (one.equals(two) – student2018 Sep 28 '18 at 12:46

1 Answers1

0

If you have a NPE in one.equals(two) is becausw one is null. One is a reference pointing to nothing, you are not setting it in your main method.

I suppose that you set this reference in the constructor of ArrayPalindrome. You have to call in your main method the constructor ArrayPalindrome.

Remeber that your constructor should be change, not to use this.one=one where you are saying the one variable has to refer to one and one is nothing. You have to create an object f.e: one=new ArrayStack().

You are not calling isPalindrom anywhere you have only defined It.

vmrvictor
  • 683
  • 7
  • 25