Skip to content
Advertisement

How to remove certain items from a Stack [closed]

I have a homework problem in which I need to remove all the green items from the stack.

Here is my code:

import java.util.*;
import java.io.*;

public class Pex
{
public static void main(String[] args)
{
Stack stack1 = new Stack(); 

addPez(stack1);
removeGreen(stack1);
System.out.println(printStack(stack2));
}

public void addPez(Stack stack1)
{
    stack1.push("yellow");
    stack1.push("red");
    stack1.push("green");
    stack1.push("green");
    stack1.push("yellow");
    stack1.push("yellow");
    stack1.push("red");
    stack1.push("green");
}

public static void removeGreen(Stack stack1)
{
Stack temp = new Stack();
while (!stack1.isEmpty()){
  String check = (String)(stack1.pop());
    if(check.equals("green")){
    stack1.pop();}
    else{
    temp.push(check);}
}
Stack stack2 = new Stack();
while (!temp.isEmpty()){
  String tmp = stack1.pop();
  stack2.push(tmp);}
}

public static void printStack(Stack stack2)
{
Stack xerox = stack2.clone();
while (!xerox.isEmpty()){
  System.out.println(xerox.pop());}
}
}

Can someone point me in the right direction? I think I’m almost there.

I also need to figure out why I am getting some errors.

Here are the errors:

Pex.java:10: non-static method addPez(Stack) cannot be referenced from a static context
addPez(stack1);
^
Pex.java:12: cannot find symbol
symbol  : variable stack2
location: class Pex
System.out.println(printStack(stack2));
                              ^

Pex.java:39: incompatible types
found   : java.lang.Object
required: java.lang.String
  String tmp = stack1.pop();
                         ^

Pex.java:45: incompatible types
found   : java.lang.Object
required: Stack
Stack xerox = stack2.clone();
                          ^

./Stack.java:69: cannot find symbol
symbol  : variable stack1
location: class Stack
    stack2 = (Stack)(stack1.clone());
                     ^

5 errors

Advertisement

Answer

public static void main(String[] args) {
    Stack stack1 = new Stack();

    addPez(stack1);
    removeGreen(stack1);
    printStack(stack1); //stack2 is not defined and remove println statement
}

public static void addPez(Stack stack1) {//make addPez as static
    stack1.push("yellow");
    stack1.push("red");
    stack1.push("green");
    stack1.push("green");
    stack1.push("yellow");
    stack1.push("yellow");
    stack1.push("red");
    stack1.push("green");
}

public static void removeGreen(Stack stack1) {
    Stack temp = new Stack();
    while (!stack1.isEmpty()) {
        String check = (String) (stack1.pop());
        if (check.equals("green")) {
            //stack1.pop();
        } else {
            temp.push(check);
        }
    }
    //Stack stack2 = new Stack();
    while (!temp.isEmpty()) {
        String tmp = (String)temp.pop();
        stack1.push(tmp);
    }
}

public static void printStack(Stack stack1) {
    Stack xerox = (Stack)stack1.clone();
    while (!xerox.isEmpty()) {
        System.out.println(xerox.pop());
    }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement