0

I'm trying to use a java Stack to push String values:names into it, derived from input from a Scanner method. I'd then want to pop the names from the stack, and after show the logical and physical elements of the stack. I believe I should be using an Array Stack but not entirely sure as I don't have much quidance or experience with Stacks.

Currently I am getting the following error when compiling the Stack.java file.

Stack.java:24: cannot find symbol
symbol  : method push(java.lang.String)
location: class java.lang.String[]
stack.push(s);

I've been researching this and trying different code for days, but as I am very new to this, and I have run out of ways to manipulate the code to make it work.

I would highly appreciate some advice...tnx!

Here is my Stack Class code:

import java.lang.*;
import java.util.*;

public class Stack {  //Create Class

        private String stack[] ;
        private int top;
        private static final int SIZE = 5;
        String name;
        Scanner scan = new Scanner(System.in);

        public Stack(int SIZE){
            stack = new String [SIZE];
            top = 0;
        }

    public void push(String s) {    //Method to Insert

        if (isStackFull())
            System.out.println ("Stack is Full ");  //If Stack full Print Full
        else {
            while (scan.hasNextLine()){
                s = scan.nextLine();
                stack.push(s);
            }
                stack[top] = s;
                top++;      
        } 
    }

    public String pop() {   //Method to Delete

        if (isStackEmpty())
            System.out.println ("Stack is Empty "); //If Stack empty print Empty
        else{
            String value = stack[top];
            top--;
            return value;
        }
        return stack[top];
    }   

    public String toString( ){      //Method print Logical Stack
            if (isStackEmpty( ))
                System.out.println ("Stack is Empty "); //If Stack empty print Empty
            else 
                System.out.println("\nThe Stack");
                String result = "";

                for (int j=0; j < top; j++)

                    result = result + stack[j].toString() + "\n";

                return result;
    }

    public boolean isStackEmpty() { //Method boolean type to check empty Stack
        return (top == 0);
    }

    public boolean isStackFull() {      //Method boolean type to check full Stack
        return (top == (SIZE-1));
    }
}

For the StackTest Code, generally I call the methods in Stack. Eg.

public class StackTest { //Create class
    public static void main(String[] args){     //Main Method
        Stack n = new Stack();  //Declare variables
        Scanner in = new Scanner(System.in);  
        String response;                        
        char x;

and

while(x != 'q' && x != 'Q'){        //While loop initiates if no q or Q char input

                switch(x){                      //Start of swtich for insert, delete, print physical, logical or default quite  
                    case 'i':                   
                        n.push();                       
                        System.out.println ("Inserted item from Stack");
                        break;
                    case 'd':
                        n.pop();
                        System.out.println ("Deleted item from Stack");
                        break;
                    case 'p':
                        n.toString();
                        System.out.println ("Printed Physical Stack ");
                        break;
2
  • stack is a String array, which has no push method. Commented Dec 1, 2016 at 0:05
  • If you need a Stack implementation that doesn't require thread-safety, ArrayDeque is preferrable. Commented Dec 1, 2016 at 0:13

2 Answers 2

1

Your push(String s) is method is calling push() on String which is incorrect, moreover the logic for push(String s) is complicated, rather it is simple as shown below in the code with comments:

public void push(String s) {
    if (isStackFull())
        System.out.println ("Stack is Full "); //If Stack full Print Full
    else {
        stack[top] = s; //just add the string to the top of the stack
        top++; //increment top
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the push method itself you are trying to call stack.push hence it is throwing error. replace the stack.push to stack[top_index]= the string.

5 Comments

Tnx all! I'll try your recommendations.
Ok...tnx. In the push method I deleted the stack.push(s) call and this resolved the compile error. Now I have a compile error in StackTest which I don't understand. It gets hung up in my switch statement: n.push()
Tnx. In push method I deleted the stack.push(s) call and this fixed the compile error. Now I have a compile error in StackTest. Its at my switch statement: n.push() and I assume all of the n.X statements. Above, with Stack n = new Stack() and then n.push() I am trying to declare a new stack n which takes an input name from a scanner method and pushes it to the stack. Not sure why it can't find the constructor Stack(). In Stack.java did I get the Stack constructor right? I wanted to construct a stack of strings that is limited to SIZE which = 5 elements.
The error is: StackTest.java:6: cannot find symbol symbol : constructor Stack(); location: class Stack; Stack n = new Stack(); StackTest.java:32: push(java.lang.String) in Stack cannot be applied to (); n.push();
Never Mind. I resolved the issue and corrected my stack constructor. Thanks All!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.