All Courses
Input and Ouput in Java

Input and Output in Java

April 30th, 2019 Java

Input and Output in Java

Usually, the user enters this data through the keyboard and views the result upon the monitor. The data of the keyboard runs over that thought also from memory to the monitor back processing. This movement of data from one volume to another is described over as a data stream which means of two types data and output stream.

System Class

System class in Java.lang contains into three fields that namely on in, out, err.

Syntax

package: java.lang
public final class System
{
//class fields and methods
}

System .in:

It’s refers to the Input Stream object value and corresponds to keyboard input point or any input source value’s specified by the host environment function area or user.

System.out:

It’s referred to the Output Stream objective volume and corresponds to the display  Output(monitor)value or any output value objective specified with the host environment or
user.

System.err:

It refers to an error output value function stream object and corresponds to the display error output(monitor) value.

How to output display on the Screen?

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

Example

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

How to Data will be Accepted from KeyBoard?

They are Three Objective

  • System.in
  • InputStreamReader
  • BufferedReader

InputStreamReader and the BufferedReader value are classes into a java.io package.

System.in:

The data value will be received to the form of bytes value from the keyboard point function by System.in it’s an InputStream objective

InputStreamReader:

It reads bytes value and decodes value them into characters.

BufferedReader:

Then finally the value of BufferedReader object function reads text value from a character of input stream value, buffering characters it’s to provide for the effective reading of characters value, arrays, and lines.

InputStreamReader inp = new InputStreamReader(system.in);
BufferedReader br = new BufferedReader(inp);

Sample Program Code

import java.io.*;
public class InputOperationsExample
{
public static void main(String args[])
IOException value
{
System.out.println("Student Name: ");
InputStreamReader inp = new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inp);
String name = br.readLine();
System.out.println("Your name is: "+student name);
}
}
Enter Student Name:
Karthick
Your name is: Karthick

How to display the Read() and Readline() Method

Read()

BufferedReader class includes methods before-mentioned as read() and readline() It can be used to read the data value from BufferedReader object value.
read() – It’s a single character value
char c = (char)br.read();

ReadLine()

ReadLine() it reads a line of text value function of the BufferedReader objective value ‘or’ and returns data the string value function. So no need of casting here.
readline() – It reads a line of text value
String s = br.readline()

How Screen Display to Stoning Numeric Data Value from Bufferedreader Objective

All numeric data value such as integer, float value and double value function are read with readLine in the form of string data.
To retrieve integer data value

String no = br.readline()
int value = Integer.parseInt(no)
Data Statement ———————-
Integer -> Integer.parseInt(br.readline() );
Long -> Long.parseLong(br.readline() );
Float -> Float.parseFloat(br.readline() );
Double -> Double.parseDouble(br.readline() );
Boolean -> Boolean.parseBoolean(br.readline() );
Byte -> Byte.parseByte(br.readline() );
Short -> Short.parseShort(br.readline() );

How to using Scanner Class using Java

Java.util package -> It provides Scanner class to read the data input value from the keyboard.
Scanner s =new Scanner(System.in);

Below table shows programs to understand respective data types value from Scanner object.

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

We use next() value method is referred to the Scanner class to read a String from Scanner object s.

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

0 responses on "Input and Output in Java"

Leave a Message

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