简体   繁体   中英

Using Java Stack with String elements

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;

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
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM