Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:42:36+00:00 2026-06-10T19:42:36+00:00

MyStack() { Vector<Integer> v=new Vector<Integer>(10,2); } void push(int n) { v.addElement(n); } void pop()

  • 0
MyStack()
{
    Vector<Integer> v=new Vector<Integer>(10,2);
}

void push(int n)
{
    v.addElement(n);
}

void pop()
{
    if(v.isEmpty())
        System.out.println("Stack underflow!");
    else
        System.out.println(v.elementAt(0));
}

void display()
{
    for(int i=0;i<v.size();i++)
        System.out.print(v.elementAt(i) +" ");
}

}

class StackDemo
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        MyStack s=new MyStack();
        int option=0;
        do
        {
            System.out.println("1: Push\n2:Pop\n3:Display\n4:Quit");
            System.out.println("Enter your option: ");
            option=in.nextInt();
            switch(option)
            {
                case 1:
                {
                System.out.println("Enter an integer:");
                int n=in.nextInt();
                s.push(n);break;
                }

                case 2:s.pop();break;

                case 3:s.display();break;

            }

        }

    while(option!=4);

    }

}

// throws an error: variable v not found. Any help would be much appreciated.Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:42:38+00:00Added an answer on June 10, 2026 at 7:42 pm

    It looks like v is being created locally in your constructor instead of as a member of your class.

    Try defining v as a class member and then simply assign it in your constructor.

    class MyStack {
        Vector<Integer> v;
    
        public MyStack() {
            v = new Vector<Integer>(10,2);
        }
    }
    

    Or just assign it when you define it:

    class MyStack {
        Vector<Integer> v = new Vector<Integer>(10,2);
    }
    

    Check out the Java tutorial on class members.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void deleteAllNodes () { stack <parentBranch *> mystack; // `trunk` is the existing head
public boolean isPalindrome() { Stack myStack = new Stack(); for(Node current = head; current!=null;
template<class T> class mStack { private: vector<T> a; vector<T>::iterator top; public: void push(T); T
Stack^ my_stack=gcnew Stack; my_class^ a=gcnew my_class; my_stack->Push(a); my_stack->Push(gcnew my_class); my_stack->Pop(); //will it cause memory
I need to inherit paramsPrepareParamsStack interceptor stack into mystack and need to override validation
I try to exclude the jquery ui stack function JS Code stack('myStack'); function stack(selector)
If in C++/cli I write ref class test { void method() {< std::vector<float> my_stack_vector;
Let say here is my stack layout View3 --> Top of the stack View2
I want to get a detailed log about my stack trace. I can get
Hey all! Having a little trouble with my stack. Im trying to print each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.