How to find a random entered number in a stack -
so i'm doing creating stack , find way search random number in stack. if code is:
stack thisstack = new stack(); scanner userinput = new scanner(system.in); = userinput.nextint(); b = userinput.nextint(); c = userinput.nextint(); showpush(thisstack, a); showpush(thisstack, b); showpush(thisstack, c);
my question how find number user inputs, , able throw exception if number isn't there. if stack user inputs [42, 543, 12] , 42 shows 42 if 43 says "number not in stack"
since stack class inherits vector class, have access contains() method.
contains()
returns true
if object in stack; false
if not, 1 trivial implementation of showpush() method 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
search target, , stack
... stack.
one thing note here when declaring stack (since generic type) have declare type arguments (to ensure type safety), bears following change in thisstack
's declaration:
stack<integer> thisstack = new stack<integer>();
you can check mureinik's answer here.
edit/appendix
just completeness, have added small demo 2 different types of user input (int , float) being pushed stack. possible when declaring stack more general <object>
type argument, 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 way or declare showpush static stack<object> thisstack = new stack<object>(); scanner userinput = new scanner(system.in); int a,pb; float b,pa; // user input system.out.print("enter 2 numbers (int, float):"); try { = userinput.nextint(); b = userinput.nextfloat(); } catch (exception e) { system.out.println("exception : "+e.tostring()); return; } // push user input stack system.out.print("\npushing user input 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 stack lifo)"); // check stack system.out.println("checking stack..."); j.showpush(thisstack, a); j.showpush(thisstack, b); // push b in stack system.out.print("\npushing "+b+" 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 2 numbers (int, float):1 23.4 pushing user input stack... checking stack... found 1 in stack found 23.4 in stack popping stack contents... popped contents : [ 23.4, 1 ] (notice stack lifo) checking stack... 1 not in stack 23.4 not in stack pushing 23.4 stack... checking stack... 1 not in stack found 23.4 in stack
Comments
Post a Comment