Ways to Read Input from Keyboard/User/Console by Java Program


Java Support 4 Ways of  read  input from keyboard:

  • Scanner Class (In java.util package)
  • BufferReader and InputStreamReader (java.io package)
  • DataInputStream (Java.io package)
  • Console Class (Some IDE doesn’t support it For Example : Eclipse)

How Data Accepts from Keyboard ?

For accepting data from keyboard by console we need three objects:

  1. System.in
  2. InputStreamReader
  3. BufferedReader

The data is received in the form of bytes from the keyboard by System.in which is an InputStream object.Then the InputStreamReader reads bytes and decodes them into characters.Then finally BufferedReader object reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

 

InputStreamReader and BufferedReader are classes of java.io package.

Below Java Program showing all four ways of taking user input from console.

 

 

package program.common;

import java.io.BufferedReader;
import java.io.Console;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class UserInputWays {

	public static void main(String[] args) {
		Way1ByScanner();
		System.out.println("=====================================================");
		Way2ByBufferReader();
		System.out.println("=====================================================");
		Way3ByDataStream();
		System.out.println("=====================================================");
		Way4ByConsole();
		System.out.println("=====================================================");
	}

	private static void Way1ByScanner() {
		System.out.println("Way 1 : By Scanner Class");
		Scanner scan = new Scanner(System.in);

		System.out.println("Enter a text string");
		String s = scan.nextLine();
		System.out.println("You entered text string " + s);

		System.out.println("Enter an integer value");
		int a = scan.nextInt();
		System.out.println("You entered integer value " + a);

		System.out.println("Enter a float value");
		float b = scan.nextFloat();
		System.out.println("You entered float value " + b);

	}

	private static void Way2ByBufferReader() {
		System.out.println("Way 2 : By BufferReader and InputStreamReader");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter a text string");
			String s = br.readLine();
			System.out.println("You entered text string " + s);

			System.out.println("Enter an integer value");
			int a = Integer.parseInt(br.readLine());
			System.out.println("You entered integer value " + a);

			System.out.println("Enter a float value");
			float b = Float.parseFloat(br.readLine());
			System.out.println("You entered float value " + b);

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	private static void Way3ByDataStream() {
		System.out.println("Way 3 : By DataStream");
		try {
			DataInputStream dis = new DataInputStream(System.in);

			System.out.println("Enter a text string");
			String s = dis.readLine();
			System.out.println("You entered text string " + s);

			System.out.println("Enter an integer value");
			int a = dis.readInt();
			System.out.println("You entered integer value " + a);

			System.out.println("Enter a float value");
			float b = dis.readFloat();
			System.out.println("You entered float value " + b);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * Eclipse doesn't support Console Class to test that use command prompt
	 */
	private static void Way4ByConsole() {
		System.out.println("Way 4 : By Console Class");
		try
		{
		Console console = System.console();

		System.out.println("Enter a text string");
		String s = console.readLine();
		System.out.println("You entered text string " + s);

		System.out.println("Enter an integer value");
		int a = Integer.parseInt(console.readLine());
		System.out.println("You entered integer value " + a);

		System.out.println("Enter a float value");
		float b = Float.parseFloat(console.readLine());
		System.out.println("You entered float value " + b);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

}

Output

Way 1 : By Scanner Class
Enter a text string
saurabh Gupta
You entered text string saurabh Gupta
Enter an integer value
35
You entered integer value 35
Enter a float value
80.2
You entered float value 80.2
=====================================================
Way 2 : By BufferReader and InputStreamReader
Enter a text string
Saurabh Gupta
You entered text string Saurabh Gupta
Enter an integer value
35
You entered integer value 35
Enter a float value
80.2
You entered float value 80.2
=====================================================
Way 3 : By DataStream
Enter a text string
Saurabh Gupta
You entered text string Saurabh Gupta
Enter an integer value
35
You entered integer value 859114762
Enter a float value
80.2
You entered float value 4.2004693E-5
=====================================================
Way 4 : By Console Class
Enter a text string
=====================================================
java.lang.NullPointerException
	at program.common.UserInputWays.Way4ByConsole(UserInputWays.java:91)
	at program.common.UserInputWays.main(UserInputWays.java:19)

More

For more Algorithms and Java Programing Test questions and sample code follow below links

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 10 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Algorithm, Data Structure, Java, Programming Test and tagged , , , , , , , . Bookmark the permalink.

One Response to Ways to Read Input from Keyboard/User/Console by Java Program

  1. Pingback: Frequently Asked Algorithms and Programs in Interviews | Facing Issues On IT

Comments are closed.