System.in and Closing the Input Stream - touniez/comp402doc GitHub Wiki
Your program can ask for text input from the user by invoking
System.in
. You can also use the System.in.read()
method in the
Interactions Pane directly. When the input box appears, type your text
and then either press Return.
You can choose to close the input stream by selecting the menu item
"Tools, Interactions & Console, Close System.in", or by pressing the
keyboard shortcut for it, which is Ctrl-D
. The shortcut is labeled
Close System.in
in the Key Bindings section of the preferences.
Here is an example of closing the input stream. The text in square brackets was entered by the user.
Welcome to DrJava. Working directory is
/Users/Shared/drjava > System.in.read() [1] 49 > System.in.read() 10 >
System.in.read() // press Ctrl-D now [] -1 >
The user first types '1' and then presses Return. This lets DrJava read a 49, which is the ASCII code for the character '1', and then 10, which is the ASCII code for the new line created by Return. In the second input box, the user pressed Ctrl-D immediately to close the input stream. This lets DrJava read -1, indicating of the end of the stream.