-1

I have to implement a program using the Stack class which allows a user to enter a positive integer,n, followed by n integers. Then the program should allow the user to enter another integer, val after which the program should display the last value which is greater than val. From my understanding of the program, I figured out that it should compare each element from the top of the stack to the val. Therefore to compare each element to val, it should iterate through the values in the stack starting from the top. I don't really know how to make this work so would be pleased if I could get any help. Here is my program:

import java.util.*;
public class MyClass{

public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);

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

    int n, num, val;
    System.out.println("Please enter n.");
    n=sc.nextInt();

    for(int i=0; i<n;i++){

        num=sc.nextInt();
        NumsInStack.push(num);

    }

    System.out.println("Enter a value.");
    val=sc.nextInt();

    for(int i=0; i<NumsInStack.size();i++){


        if(NumsInStack.peek()>val)
            System.out.println("The number greater than "+val+" is "+NumsInStack.peek());

    }


  }
}        
Tia
  • 1,220
  • 5
  • 22
  • 47
  • _apparently the stack class doesn't really allow iterators_ What makes you think so? – Sotirios Delimanolis Sep 28 '15 at 14:43
  • How doesn't it allow iterators? It's a `Vector` subclass. http://stackoverflow.com/q/12957123/438992 – Dave Newton Sep 28 '15 at 14:44
  • 2
    Unrelated, `Vector` class! Haven't thought about *that* for awhile. – Dave Newton Sep 28 '15 at 14:44
  • replace peek() with pop(). – ilj Sep 28 '15 at 14:50
  • 1
    [I'm out of votes.](http://stackoverflow.com/questions/12957123/how-would-i-iterate-a-stack-in-java) – Sotirios Delimanolis Sep 28 '15 at 14:52
  • This is the "old" stack, based on the "old" Vector. Using these classes can have a significant performance hit because Vector is synchronized on most method calls, which sounds like a good thing until you gain experience and understand that you really need the synchronization at a higher level. That's why nearly every class now teaches ArrayList as the preferred data structure to use if you are dealing with Lists of data. – Edwin Buck Sep 28 '15 at 14:52

3 Answers3

1

The stack you are using is also a Vector, so it gets all the methods a stack has and all the methods a Vector has.

Iterator<Integer> stackIterator = NumsInStack.iterator();

while (stackIterator.hasNext()) {
  Integer item = stackIterator.next();
  // do whatever
}

Keep in mind that it is much better to not name variables starting with an uppercase letter (it makes them hard to differentiate between classes and variables) unless the variable is a constant, in which case, name it with all uppercase letters, and underscores to separate out the words.

There are many variants on how to loop through; and Vector supports nearly all of them, which means that you don't have to use an iterator. Look at the bottom of the Javadoc for Stack to see all the methods Stack inherits (you're probably scrolling over them)

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Thank you for your ans but basically I have to check the elements starting from the **top** against the value `val` and print out the last element which is greater than `val`.. I edited the title of the question and its body – Tia Sep 28 '15 at 15:42
  • So you have a while loop in my example. Check the values within it. – Edwin Buck Sep 28 '15 at 20:00
0

Stack class allows Iterator to iterate over its elements.

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

Iterator<Integer> iter = NumsInStack.iterator();

while (iter.hasNext()){
    System.out.println(iter.next());
}

Other than that,You can use enhanced-for loop

for(Integer i : NumsInStack)
{
    System.out.println(i);
}

See Also

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
0

If you're using Java 8, Stack extends Iterable, so just iterate:

for (int x : NumsInStack) { ... }

You can also refer to the documentation.

Lawrence
  • 352
  • 4
  • 14