Q:

java error: cannot find symbol stack.size()

I'm getting confused. I checked the parameters of the size() method in java, and I know that it is part of the Vector class which the Stack class inherits. Why isn't this method working on my stack? I want to know the size of the stack. Is there any other way to get the size if not this way?
import java.util.Vector;

public class InfixCalculator
{
...
    Stack<String> stack = new Stack<String>();

    while((stack.size() > 0) && (stack.peek() != "(")
}

             ^ symbol: method size()
               location: variable stack of type Stack<String>

public class Stack<String> implelments StackInter<String>
{
     public void push()
    {
        ...
    }   
    public String pop()
    {
        ...
    }
    public boolean isEmptyStack()
    {
        ...
    }
    public String peek()
    {
        ...
    }
}

I'm sorry. I should've mentioned that I can't import the Stack class. I had to create my own class stack. Is there a way to get the size of the stack without creating a method for it in my Stack class?

A:

This is a sample code to get the size of your stack if this is what you intend to do-
Stack<String> sk=new Stack<String>();

sk.push("a");
sk.push("c");
sk.push("e");
sk.push("d");

int sizeOfStack=sk.size();

System.out.println("Size of Stack :"+sizeOfStack);

Output
Size of Stack :4

Source: http://www.easywayserver.com/blog/java-how-to-get-size-of-stack/
Further reading: http://morbidjava.blogspot.in/2012/03/how-to-impliment-stack-data-structure.html

