There are plenty of ways to do this. I'd advise you avoid using relics of the past like
Stack in favor of using the newer
Deque collection. I quote:
...
A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
...
You can even convert a Deque into a LIFO Queue using
Collections.asLifoQueue(), if need be.
Anyways, here's one way to solve your problem.
import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Scanner;
public final class EchoSequences {
private EchoSequences() { }
private static void print(final Deque<Integer>... sequences) {
final PrintStream output = System.out;
for (final Deque<Integer> seq : sequences) {
final Iterator<Integer> cursor = seq.iterator();
while (cursor.hasNext()) {
output.print(cursor.next());
output.print(' ');
}
output.println();
}
}
public static void main(String[] argv) {
final Deque<Integer> positives = new ArrayDeque<Integer>();
/* technically not strictly negatives as it also may contain 0 */
final Deque<Integer> negatives = new ArrayDeque<Integer>();
final Scanner input = new Scanner(System.in);
while (input.hasNextInt()) {
int n = input.nextInt();
if (n > 0) {
positives.add(n);
} else {
negatives.push(n);
if (n == -1) {
break;
}
}
}
print(positives, negatives);
}
}
On the topic of your own code, I'd advise not calling top()/pop() separately, as you can merely replace the top() call with a pop() and keep the same functionality.