Thursday 18 August 2022

Accepting Input from the Keyboard in Java

 Input represents data given to a program. There are so many classes in java for reading input into program. Here I will discuss to most widely used to classes.

A stream is required to accept input from the keyboard. A stream represents flow of data from one place to another place. Basically there are two types of streams in java: input stream and output stream.

All streams are represented by classes in java.io package. Keyboard is represented by a filed 'in' in the System class. When we write System.in, we are representing a standard input device i.e, keyboard, by default. System class is found in java.lang package and has three fields as shown below. All these fields represent some type of stream.

  1. System.in: This represents InputStream object, which  by default represents standard input device like keyboard.
  2. System.out: This represents PrintStream object, which by default represents standard output device like monitor.
  3. System.error: This filed also represents PrintStream object, which by default represents standard output device like monitor. But it used to display error message.

To accept data from the keyboard i.e, System.in, we need to connect it to an input stream as some input stream is need to read data.

Connect the keyboard to an input stream object. Here, we can use InputStreamReader that can read data from the keyboard like

                     InputStreamReader obj = new InputStreamReader (System.in);

In this statement, we are creating InputStreamReader object and connecting the keyboard to it.

In order to read different types of data from stream, we need to connect InputStreamReader to BufferedReader as it has got methods for this purpose.

                   BufferedReader br = new BufferedReader(obj);

Now using different methods of BufferedReader class we read data from stream. The two most widely used method for reading are: 

read(): Used to read single character from stream.

readLine(): Used to read entire line of text from stream.

Reading input with java.util.Scanner class

We can use Scanner class of java.util package to read input from the keyboard or a text file. When the Scanner class receives input, it breaks the input into several pieces, called tokens. These  tokens can be retrieved from the Scanner object using the following methods:

  •  next() - to read a string 
  • nextByte() - to read byte value
  • nextInt() - to read an integer value.
  • nextFloat() - to read float value
  • nextLong() - to read long value
  • nextDouble() - to  read double value.

To read input from keyboard, we can use Scanner class as:

Scanner scr = new Scanner(System.in);

Now, using this 'scr' object and above methods one can read different types of data from keyboard.

Example:

import java.util.Scanner;
class InputDemo 
{
	public static void main(String[] args) 
	{
		Scanner scr = new Scanner(System.in);

		System.out.println("Enter employee Id");
		int id = scr.nextInt();

		System.out.println("Enter employee Name");
		String name=scr.next();

		System.out.println("Enter employee salary");
		float salary = scr.nextFloat();

		System.out.println("Employee Id=" + id);
        System.out.println("Employee Name=" + name);
        System.out.println("Employee salary=" + salary);

	}
}

Output:


It is good idea to use Scanner class instead of all other input class.


0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top