Java Keyboard Input

Java Keyboard Input


For every language keyboard input is a must. That is, the user can feed data to a running Java program using keyboard. Java Keyboard Input involves I/O Streams.

In Java Keyboard Input can be taken in three ways using three different classes. Let us explore one and then see the other two. Here Scanner class is used which is easier to write code. Other two styles take extra lines of parsing.

Scanner is a class introduced with JDK 1.5. It is placed in java.util package to be nearer to Data Structures and not in java.io package.

Following example on Java Keyboard Input takes from the Student name, circle radius and rectangle height and length.

Java Keyboard InputOutput Screenshot on Java Keyboard Input

System.in is connected OS stream passing from keyboard to CPU. Scanner object is connected to System.in to read from keyboard. nextLine(), nestInt() and nextDouble() methods of Scanner class returns user input as a string, int and double that can be used in code directly for arithmetic calculations.

Three programs are given on Keyboard input using different classes and I/O Streams.

1. Keyboard Input – DataInputStream
2. Keyboard Input – Character Streams – BufferedReader
3. Keyboard Input – java.util.Scanner – No parsing

Leave a Reply

Your email address will not be published. Required fields are marked *