Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the End Of File/Stream keyboard combination to use with System.in.read()

Apologize if this trivial question has already been answered, I cannot find it at SO. Reading lines from the IDE console with this Java trivial code (Windows 7 and Eclipse Kepler):

int v;
try { while ((v = System.in.read()) != -1) System.out.println(v); }
catch (IOException e) { ; }

How the user can make the value v equal to -1? (I've tried Ctrl + d-z-x-c-s-e and other keys without repeatable behavior, but the loop is interrupted randomly)

like image 758
mins Avatar asked May 19 '14 16:05

mins


2 Answers

Control + D should send the EOF character as excepted , but it is an bug in Eclipse.

One of the user reported as

    In Kepler 4.3 eclipse.buildId=4.3.0.M20130911-1000 on Linux the problem 
    still exists in the Java console. I found the following workaround:

    If you leave the console to focus on another view, and then refocus on the console,
    then Ctrl-D (EOF) works as expected.

Follow here


When using Eclipse in Windows, Control + Z sends the EOF character.

like image 57
Mani Avatar answered Sep 30 '22 22:09

Mani


Testing this out using Groovy in a Windows command prompt, ctrl-D doesn't work while ctrl-C does:

C:>groovy -e "while(v = System.in.read()){ println v }"
^D
4
13
10
-1
Terminate batch job (Y/N)? y

First I hit ctrl-D then enter, resulting in output ctrl-D, 4, 13, 10 (the last three being EOT, CR, LF, I guess, not sure what ^D amounts to in this case). Then I tried ctrl-C and the '-1' end-of-input was sent. So it seems this is dependent on the shell as Dev says.

like image 44
rom99 Avatar answered Sep 30 '22 20:09

rom99