x

Java – Input/Output

PREV     NEXT

  • Each and every program needs data which is to be processed. Such datas are represented as inputs and the final resulting data which we get after processing is represented as output.
  • Usually user enters the data through keyboard and view the result on the monitor. The data from the keyboard flows to the memory and from memory to the monitor after processing.This flow of data from one place to another is referred as data stream which is of two types input and output stream.

System class:

 System class in Java.lang contain three fields namely in,out,err.


package: java.lang
public final class System{
//class fields and methods
}
System.in refers InputStream object and corresponds to keyboard input or any input source specified by the host environment or user.
System.out refers OutputStream object and corresponds to display output(monitor) or any output destination specified by the host environment or user.
System.err refers error output stream object and corresponds to display error output(monitor) or anyoutput destination specified by the host environment or user.

How to display the output onto the screen?

We use print() and println() method of System.out to display the output.

System.out.print(“Hello”); //keeps the cursor on the same line after displaying output
System.out.println(“Hello”); //keeps the cursor on the next line after displaying output

How data is accepted from keyboard ?

We need three objects,

  1. System.in
  2. InputStreamReader
  3. BufferedReader
  • InputStreamReader and BufferedReader are classes in java.io package.
  • 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 inp = new InputStreamReader(system.in);
BufferedReader br = new BufferedReader(inp);

Example program:

C:\> javac InputOperationsExample.java
C:\> java InputOperationsExample
Enter your name:
Tamil
Your name is: Tamil

read() and readLine() methods:

BufferedReader class contains methods such as read() and readline() which are used to read the data from BufferedReader object.
read() – reads a single character


char c = (char)br.read();

br.read() reads a single character from the BufferedReader object ‘br’ but returns its ASCII value which is an integer. so we use typecast to convert an integer to character by using (char) before br.read().

readline() – reads a line of text

String s = br.readline()

br.readLine() reads a line of text from the BufferedReader object ‘br’ and returns string. So no need of casting here.

Storing numeric data from BufferedReader object:

All numeric datas such as integer, float and double are read by readLine in the form of string data.

String no = br.readline()

To retrieve integer, we use

int value = Integer.parseInt(no)

Since typecasting is done only between data types and String is a class. we cannot typecast String to an integer so we use parseInt method of Integer Wrapper class. Similarly for other data types, refer the below table

Data Type
Statement
Integer int value = Integer.parseInt(br.readline() );
Long long value = Long.parseLong(br.readline() );
Float float value = Float.parseFloat(br.readline() );
Double double value = Double.parseDouble(br.readline() );
Boolean boolean value = Boolean.parseBoolean(br.readline() );
Byte byte value = Byte.parseByte(br.readline() );
Short short value = Short.parseShort(br.readline() );

Using Scanner class

Java.util package provides Scanner class to read the input from the keyboard.

Scanner s =new Scanner(System.in);

Below table shows methods to read respective data types from Scanner object.

Data Type
method to read Datatype
String
next()
Integer nextInt()
Long nextLong()
Float nextFloat()
Double nextDouble()
Byte nextByte()

We use next() method of Scanner class to read a String from Scanner object s.

Scanner s =new Scanner(System.in);
String str = s.next();
int no = s.nextInt();

PREV     NEXT



Like it? Please Spread the word!