1

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?

Timothy Lawman
  • 2,194
  • 6
  • 24
  • 33
  • 2
    If you're using console input as the input buffer, it will never be empty. You would need to program in some exit code that the user can enter that exits your program. – Mage Xy Oct 13 '15 at 19:56
  • Try pressing CTRL-Z https://en.wikipedia.org/wiki/End-of-file that is meant to be a way to end the console input. – zapl Oct 13 '15 at 20:06
  • `Scanner` in `StdIn` is most probably using `System.in` stream which JVM process is using to read data usually from console. But when can we be 100% sure that there will be no input from user in that stream? Since user can be in the middle of writing some data via console `System.in` needs to wait until a) user will press enter and send his data b) System.in will detect that stream is closed (which means no more data can be read from it). In case B you can use something like http://stackoverflow.com/a/33084716/1393766 – Pshemo Oct 13 '15 at 20:12
  • ctrl Z did the trick ! Does it stop current program or actually tell scanner to return true? – Timothy Lawman Oct 13 '15 at 20:13
  • ctrl Z is not very nice design decision. It is better to let user provide some special value which will make program exit. – Pshemo Oct 13 '15 at 20:16
  • yes I agree but the class was written by a world renowned professor of java which claims it is a cleaner interface! All the suggestions work so I don't know why it used at all if it is supposed to be cleaner? – Timothy Lawman Oct 13 '15 at 20:17
  • what exactly does ctrl + z do, does it tell scanner it is the end of input or just halt the current running process? – Timothy Lawman Oct 13 '15 at 20:19
  • I found this :In some operating systems, Control+Z is used to signal an end-of-file, and thus known as the EOF character (more accurately: the EOF control code), when typing at a terminal, terminal emulator, MS-DOS command line, or Win32 console. – Timothy Lawman Oct 13 '15 at 20:21

1 Answers1

0

The answer was: when it hangs at the windows prompt after processing the arguments type ctrl + z which is Windows EOF for standard input to tell the scanner there is no more input.

Timothy Lawman
  • 2,194
  • 6
  • 24
  • 33
  • Ctrl+Z may prevent you from reading from console again. If you want to have something like `while(hasMoreElements){doSomethingInLoop();} doSomethingAfterLoop(); while(hasMoreElemenets){doAnotherThingInLoop();}` then in second loop `doAnotherThingInLoop()` will most probably be not executed because `hasMoreElemenets` will start returning `false`. That is why it is better to either read until some special value will be provided which should make our flow of control exit from loop, or predefine earlier how many values we want to read from user. – Pshemo Oct 13 '15 at 20:50