4

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)

mins
  • 6,478
  • 12
  • 56
  • 75

3 Answers3

14

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.

lucasvw
  • 1,345
  • 17
  • 36
Mani
  • 3,274
  • 2
  • 17
  • 27
  • 1
    Thanks a lot and bravo! I must use Ctrl-Z (Windows). Changing the view, for instance I select the Problems tab and then Console, allows CTRl-Z recognition... wow. – mins May 19 '14 at 16:38
  • 5 years later and this still isn't fixed on the new 2021-03 release, and it makes the run configuration option for automating standard require manually sending an EOF character. – DaveTheMinion Mar 18 '21 at 13:35
1

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.

rom99
  • 709
  • 4
  • 14
  • 2
    Ctrl-C doesn't work for me, but Ctrl-Z is ok (within Eclipse console). I've the bug mentioned by Mani, and need to change the focused view to have this input taken into account. – mins May 19 '14 at 16:43
-1

Use the control-D character (type D while holding down the control key) to indicate to a program reading from the standard input stream that you have finished entering input. The control-D character is often written as ^D.

Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25