Check if a stack of Integers contains a sequence of consecutive values Write a method isConsecutive that takes a stack of integers as a
  parameter and that returns whether or not the stack contains a
  sequence of consecutive integers starting from the bottom of the stack
  (returning true if it does, returning false if it does not).
  Consecutive integers are integers that come one after the other, as in
  5, 6, 7, 8, 9, etc. So if a stack s stores the following values:
  
  bottom [3, 4, 5, 6, 7, 8, 9, 10] top
  
  Then the call of isConsecutive(s) should return true. If the stack had
  instead contained this set of values: 
  
  bottom [3, 4, 5, 6, 7, 8, 9, 10, 12] top
  
  Then the call should return false because the numbers 10 and 12 are
  not consecutive. Notice that we look at the numbers starting at the
  bottom of the stack. 
  
  The following sequence of values would be consecutive except for the
  fact that it appears in reverse order, so the method would return
  false: bottom [3, 2, 1] top
  
  Your method must restore the stack so that it stores the same sequence
  of values after the call as it did before. Any stack with fewer than
  two values should be considered to be a list of consecutive integers.
  You may use one queue as auxiliary storage to solve this problem.


I'm looking for ways to improve my future code. I was also wondering if it's better practice to use generics in cases like these? I'll also accept other methods of solving this problem as the correct answer. My main goal is to try to take away something that will make me a better programmer; if you have advice to shed, please don't hesitate to share it!

public boolean isConsecutive(Stack&lt;Integer&gt; s) {
    boolean consecutive = true;
    Queue&lt;Integer&gt; q = new LinkedList&lt;Integer&gt;();
    while (!s.isEmpty()) {
        q.offer(s.pop());
    }

    while (!q.isEmpty()) {
        s.push(q.poll());
    }

    while (!s.isEmpty()) {
        q.offer(s.pop());
    }

    while (q.size() &gt; 1) {
        int first = q.poll();
        int second = q.poll();
        consecutive &amp;= (second - first == 1);
        s.push(first);
        s.push(second);
    }

    if (!q.isEmpty()) {
        int first = q.poll();
        int second = s.peek();
        consecutive &amp;= (second - first == 1);
        s.push(first);
    }
    return consecutive;
}
Bug 1

Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(isConsecutive(stack));


returns false.

Bug 2

Your method throws EmptyStackException on stack of size 1.

Advice

I suggest you declare your method as static. That way the client programmer does not need to instantiate an object.

Alternative implementation

This is just a slight modification of my answer to your previous stack related question:

public static boolean isConsecutiveV2(Stack&lt;Integer&gt; stack) {
    if (stack.size() &lt;= 1) {
        return true;
    }

    Stack&lt;Integer&gt; aux = new Stack&lt;&gt;();

    aux.push(stack.pop());

    while (!stack.isEmpty() &amp;&amp; stack.peek() == aux.peek() - 1) {
        aux.push(stack.pop());
    }

    boolean consecutive = stack.isEmpty();

    // Restore the input stack:
    while (!aux.isEmpty()) {
        stack.push(aux.pop());
    }

    return consecutive;
}