Question

does anybody know how to do a method to find the occurrences of a string on...

does anybody know how to do a method to find the occurrences of a string on the stack? I am very stumped with this assignment

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Stack;

public class StackCount {

    public static int countOnStack(Stack<String> stack, String str) {
        int count = 0;
        while (!stack.isEmpty()) {
            if (stack.pop().equals(str)) {
                ++count;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("hello");
        stack.push("hi");
        stack.push("hello");
        stack.push("how");
        stack.push("are");
        stack.push("you?");
        System.out.println(countOnStack(stack, "hello"));
    }
}
Add a comment
Know the answer?
Add Answer to:
does anybody know how to do a method to find the occurrences of a string on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT