0

I have an assignment to push Strings into a stack. I created a program that will store numbers but I cannot figure out the correct way to define the array to take a string. Here is my code. My Java is rusty so I am trying to remember all this from my first java class 2 years ago. I am sure it is super simple but I cannot find anything online where strings are stored in a stack for me to see how to do it. Thanks for the help!

    public class stackx {
        private int maxSize; //number of items in stack
        private int[] stackArray;
        private int top; // top of stack

    public stackx(int arraySize) {
        maxSize = arraySize;
        stackArray = new int[maxSize];
        top = -1;
    }

    public void push(int a) {    //put value on top of stack
        if (top == maxSize - 1) 
        {
            System.out.println("Stack is full");
        } else {

            top = top + 1;
            stackArray[top] = a;
        }
    }

    public int pop() {              //take item from top of stack
        if (!isEmpty())
            return stackArray[top--]; // access item, decrement top
        else {
            System.out.println("Stack is Empty");
        }
    }

    public int peek()               //peek at the top of the stack
    {
        return stackArray[top];
    }

    public boolean isEmpty() {      //true if stack is empty
        return top == -1;
    }

    public void display() {

        for (int i = 0; i <= top; i++) {
            System.out.print(stackArray[i] + " ");
        }
        System.out.println();
    }
    } // End class stackx


**Driver class Here**
        public class practicestack {

        public static void main(String[] args) {
            stackx newStack = new stackx(5);
            newStack.push(redShirt);
            newStack.push(greenShirt);
            newStack.push(yellowPants);
            newStack.push(purpleSocks);
            newStack.push(pinkSocks);
            stackx.peek();

//Display the Full Stack
            newStack.display();
//Test removing a value using pop method
            newStack.pop();

            newStack.display();
          }
       }
Scott
  • 3
  • 1
  • 4
  • 1
    Is this what you are looking for? [Declare Arrays in Java](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Sachin Kumar Jun 02 '17 at 03:46

3 Answers3

0

Your stack only takes ints. You need it to take Objects if you want to store anything at all. Java doesn't let you manipulate pointers, so you can't just use int like in C/C++. You could also use generics, e.g. public class stackx<T>, which would give you something like stackx<String> newStack = new stackx<>(5);.

ngreen
  • 1,420
  • 12
  • 21
0

This should be easy, I will provide you a small hint, if you still can't figure it out, I will post entire code when you declare something like this

private int[] stackArray;

and use this array to push and pop your items, as this is Integer array you can only use this implementation for Integers.

Now your requirement is to push and pop Strings, so essentially you should do something like this.

private String[] stackArray;

Note : Your Push and pop method will change likewise, those will be small changes

Hope this helps!

Good luck.

Vihar
  • 3,313
  • 2
  • 19
  • 40
  • Thanks! This worked. I tried this a few times but must have been missing something else. I deleted my whole code and rewrote it then it worked :) – Scott Jun 05 '17 at 00:09
0

Just modify int to String.Here is the Demo.

SpringDRen
  • 26
  • 3