I am running a program from Algorithms 1 by Sedgwick using the stdlib.jar library that has a method isEmpty()
which is supposed to determine if there are anymore arguments left in standard input to process.
Here is the code from the source file code contained in a class called StdIn
:
public static boolean isEmpty() {
return !scanner.hasNext();
}
if I type the following arguments in after a successful compilation at the command line in Windows:
10 2 4 6 8 9 0
and press enter, the program processes the data correctly from the main function in a while loop and the line StdOut.println(p + " " + q);
works fine.
public static void main(String[] args) {
int N = StdIn.readInt();
QuickUnionUF uf = new QuickUnionUF(N);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.connected(p, q)) continue;
uf.union(p, q);
StdOut.println(p + " " + q);
}
}
However, the while loop containing the condition (!StdIn.isEmpty())
never becomes true and after printing the answer just hangs waiting for more input. This means the while condition never determines the end of the input has been reached from the command line.
Question
How do I tell the program I have finished my arguments and there is no more input? Is there a special escape sequence I need to type at the windows command line after the last argument? Or is there a standard way to tell the scanner input of the command line arguments in finished?