Want to iterate through a stack and remove any element that contains the string "Yellow"

Viewed 14

public static void main(String[] args) {

    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);

    Stack<String> candies = new Stack<String>();

    Stack<String> clone = new Stack<String>();
    for (int i = 0; i < 12; i++) {
        candies.push(input.next());
    }
    for (int i = 0; i < 12; i++) {
        if (candies.peek().equals("Yellow")) {
            candies.pop();
        }
    }
    for (String i : candies)
        System.out.println(i);
}

The input is Pink, Orange, Yellow, Green, Yellow, Yellow, Red, Pink, Pink, Pink, Yellow, Green

But I want it to output Pink, Orange, Green, Red, Pink, Pink, Pink, Green

0 Answers
Related