0

So what I'm doing is creating a stack and then find a way to search for a random number in the stack. So basically if the code is:

    Stack thisstack = new Stack();  
    Scanner userinput = new Scanner(System.in);

    a = userinput.nextInt();  
    b = userinput.nextInt();  
    c = userinput.nextInt();

    showpush(thisstack, a);  
    showpush(thisstack, b);  
    showpush(thisstack, c);

My question is how would i find a number the user inputs, and be able to throw an exception if we look for a number that isn't there. so if the stack the user inputs is [42, 543, 12] and we look for 42 it shows 42 but if we look for 43 it says something like "number not in stack"

RStone
  • 13
  • 5
  • This is highly dependent on the language and nature of the stack. Many stack implementations (such as Java's Stack object) support search/find/indexOf. Short of that, you'd have to pop each object onto a new stack, and then pop them back onto this stack. If you're implementing your own Stack class, then you could implement a trivial linear scan of items in the stack. – Jeff Jirsa Oct 30 '15 at 21:23
  • I am using java language and putting this code in eclipse. but how would popping and re-pushing find the number the user inputs – RStone Oct 30 '15 at 21:59

1 Answers1

1

Since Stack class inherits from Vector class, you have access to its contains() method.

contains() returns true if the object is in the stack; false if not, so one trivial implementation of the showpush() method would be:

void showpush(Stack stack, int num){
    if (stack.contains(num)){
        System.out.println(num);
    } else {
        System.out.println("number not in stack");
    }
}

where num is your search target, and stack... well your stack.

One thing to note here is that when declaring a Stack (since it is a generic type) you also have to declare its type arguments (to ensure type safety), which bears the following change in thisstack's declaration:

Stack<Integer> thisstack = new Stack<Integer>();

You can also check Mureinik's answer here.


Edit/Appendix

Just for completeness, I have added a small demo with two different types of user input (int and float) being pushed into the stack. This is possible when declaring the stack with the more general <Object> type argument, as follows:

import java.util.Stack;
import java.util.Scanner;

public class Jst {
    void showpush(Stack stack, Object obj){
        if (stack.contains(obj)){
            System.out.println("Found "+obj+" in stack");
        } else {
            System.out.println(obj+" not in stack");
        }
    }

    public static void main(String[] Arguments){
        Jst j = new Jst(); //either this way or declare showpush as static
        Stack<Object> thisstack = new Stack<Object>(); 
        Scanner userinput = new Scanner(System.in);
        int a,pb;
        float b,pa;

        // get user input
        System.out.print("Enter two numbers (int, float):");
        try {
            a = userinput.nextInt();  
            b = userinput.nextFloat();
        } catch (Exception e) {
            System.out.println("Exception : "+e.toString());
            return;
        }

        // push user input into stack
        System.out.print("\nPushing user input into stack...");
        thisstack.push(a);
        thisstack.push(b);
        // check stack
        System.out.println("Checking stack...");
        j.showpush(thisstack, a);  
        j.showpush(thisstack, b);

        // pop stack contents
        System.out.print("\nPopping stack contents...");
        pa = (float)thisstack.pop();
        pb = (int)thisstack.pop();
        System.out.println("Popped contents : [ "+pa+", "+pb+" ] (Notice that Stack is LIFO)");
        // check stack
        System.out.println("Checking stack...");
        j.showpush(thisstack, a);  
        j.showpush(thisstack, b);  

        // push b back in stack
        System.out.print("\nPushing "+b+" back into stack...");
        thisstack.push(b);
        // check stack
        System.out.println("Checking stack...");
        j.showpush(thisstack, a);  
        j.showpush(thisstack, b);
    }
}

which yields:

C:\>java Jst
Enter two numbers (int, float):1 23.4

Pushing user input into stack...
Checking stack...
Found 1 in stack
Found 23.4 in stack

Popping stack contents...
Popped contents : [ 23.4, 1 ] (Notice that Stack is LIFO)
Checking stack...
1 not in stack
23.4 not in stack

Pushing 23.4 back into stack...
Checking stack...
1 not in stack
Found 23.4 in stack
Community
  • 1
  • 1
sokin
  • 794
  • 2
  • 12
  • 20