Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the code I’ve to implement the stack using an array in Java with the help of push(). But it’s not able to store values in an array.

UseStack

class UseStack{

    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);

        System.out.println("Enter the size of Stack....");

        int n = obj.nextInt();

        new Stack(n);

        while(true){

            System.out.println("1: Push");

            System.out.println("2: Show");

            int choice = obj.nextInt();;

            switch(choice){

                case 1:

                Push push = new Push(n);

                push.push();

                break;

                case 2:

                Push push1 = new Push(n);

                push1.show();

                break;

                default:

                System.out.println("Invalid Option");

                break;

            }

        }

    }

}

Stack.java

class Stack {

    public int arr[];

    public  int top;

    public  int capacity;

    Stack(int size){

        this.arr = new int[size];

        this.capacity = size;

        this.top = -1;

    }

}

Push.java

class Push extends Stack {

    Push(int size) {

        super(size);

    }

    private static Scanner obj;

    public void push(){

        obj = new Scanner(System.in);

        System.out.println("Enter Value to push...");

        int value = obj.nextInt();

        System.out.println("Value : "+value);

        if(top==capacity-1){

            System.out.println("StackOverflow");

            return;

        }

        else{

            top++;

            System.out.println("Top : "+top);

            arr[top]=value;

            System.out.println("Pushed... "+arr[top]);

        }

    }

    public void show(){

        if(top==-1){

            System.out.println("StackUnderFlow");

            return;

        }

        else{

            System.out.println("Stack Elements : ");

            for(int i=top;i>=0;i--){

                System.out.println(arr[i]+" ");

            }

        }       

    }

}

This is the output I got: 

Enter the size of Stack....

3

1: Push

2: Show

1

Enter Value to push...

5

Value : 5

Top : 0

Pushed... 5

1: Push

2: Show

1

Enter Value to push...

10

Value : 10

Top : 0

Pushed... 10

1: Push

2: Show

2

StackUnderFlow

1: Push

2: Show

The problem is it increments the top value only once after that it becomes 0 from -1. And I’m not able to store the values in the array when I push an element to it and it’s not showing me the elements. It says StackUnderflow. 

Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

As you are creating new object every time you do push and show in switch case, this makes the top to be always  initialized by -1 before operation

case 1:

Push push = new Push(n);

push.push();

break;

case 2:

Push push1 = new Push(n);

push1.show();

Break;

You have to create an object and do operation on that particular object like below:

class UseStack{

    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);

        System.out.println("Enter the size of Stack....");

        int n = obj.nextInt();

        Push push = new Push(n); // Create one object

        while(true){

            System.out.println("1: Push");

            System.out.println("2: Show");

            int choice = obj.nextInt();;

            switch(choice){

                case 1:

                push.push(); // Then do operation on that object

                break;

                case 2:

                push.show(); // Then do operation on that object

                break;

                default:

                System.out.println("Invalid Option");

                break;

            }

        }

    }

}

Interested in Java? Check out this Java tutorial by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 3, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 20, 2019 in Java by Nigam (4k points)

Browse Categories

...