Java: Get Keyboard Input


Here is a sample Java code to get keyboard input.

If you want to read user input successively, the following code will help.

This code will end if the user type Ctrl and D.

If you don’t want the user to type Ctrl and D, the following code is an alternative.

Environment

I checked the above codes work in the following environment.

  • OS: Windows 8
  • JDK: 1.7.0_51