Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why get cannot find symbol when pop in stack

I am new in data structure. I perform append and palindrome or not operation but i get Cannot Find Symbol error while initize popFromStack variable and i am stuck there…

Here down is my code:

class Node
{
    String data;
    Node next;
    Node(String data)
    {
        this.data = data;
    }
}

public class MyClass {
    Node head;
    public void append(String data)
    {
        if(head == null)
        {
            head = new Node(data);
            return;
        }
        
        Node current = head;
        while(current.next != null)
        {
            current = current.next;
        }
        current.next = new Node(data);
    }
    
    public boolean isPalindrome(Node head)
    {
        Node slow = head;
        boolean isPalin = true;
        Stack<String> stack = new Stack<String>();;
        
        while(slow != null)
        {
            stack.push(slow.data);
            slow = slow.next;
        }
        
        while(head != null)
        {
            String popFromStack = stack.pop; // here is the problem
            if(head.data == popFromStack)
            {
                isPalin = true;
            }
            else
            {
                isPalin = false;
            }
            head = head.next;
            return isPalin;
        }
    }

    public void printList()
    {
        Node current = head;
        while(current != null)
        {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    public static void main(String[] args) {
        MyClass m = new MyClass();        
        m.append("A");
        m.append("B");
        m.append("D");
        m.append("B");
        m.append("A");
        m.printList();
        
        
        System.out.println("\n");
        System.out.print("Palindrome : " + m.isPalindrome(m.head));
    }
        

Stack trace:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MyClass.java:79: error: cannot find symbol
            String popFromStack = stack.pop;
                                       ^
  symbol:   variable pop
  location: variable stack of type Stack<String>
1 error

>Solution :

pop is a method so you must add ()

String popFromStack = stack.pop();

Btw. you must not compare String with == but with calling equals.

if(head.data.equals(popFromStack))

Please read more about String comparision: https://www.javatpoint.com/string-comparison-in-java

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading