Java Input From Keyboard
Java Input From Keyboard
Java brings various Streams with its I/O package that helps the user to perform
all the input-output operations. These streams support all the types of objects,
data-types, characters, files etc to fully execute the I/O operations.
There are 3 standard or default streams that Java has to provide which are
also most common in use:
1. System.in: This is the standard input stream that is used to read
characters from the keyboard or any other standard input device.
2. System.out: This is the standard output stream that is used to
produce the result of a program on an output device like the computer
screen.
Here is a list of the various print functions that we use to output
statements:
○ print(): This method in Java is used to display a text on the
console. This text is passed as the parameter to this method
in the form of String. This method prints the text on the
console and the cursor remains at the end of the text at the
console. The next printing takes place from just here.
Syntax:
System.out.print(parameter);
Example:
class Main {
public static void main(String[]
args)
{
// using print()
// all are printed in the
// same line
System.out.print("ABC! ");
System.out.print("DEF! ");
System.out.print("GHI! ");
}
}
○
Output:
ABC! DEF! GHI!
○ println(): This method in Java is also used to display a text on
the console. It prints the text on the console and the cursor
moves to the start of the next line at the console. The next
printing takes place from the next line.
Syntax:
System.out.println(parameter);
Example:
import java.io.*;
class Main {
public static void main(String[]
args)
{
// using println()
// all are printed in the
// different line
System.out.println("ABC! ");
System.out.println("DEF! ");
System.out.println("GHI! ");
}
}
Output:
ABC!
DEF!
GHI!
import java.io.*;
public class Main{
// A Java program to demonstrate working of printf() in Java
float n = 5.2f;
n = 2324435.3f;
System.err: This is the standard error stream that is used to output all the error
data that a program might throw, on a computer screen or any standard output
device.
This stream also uses all the 3 above-mentioned functions to output the error
data:
● print()
● println()
● printf()
Example:
// Java code to illustrate standard
// input output streams
import java.io.*;
public class Main {
System.out.println("Enter
characters, "
+ " and '0' to
quit.");
char c;
do {
c = (char)inp.read();
System.out.println(c);
} while (c != '0');
}
}
Input:
abcdefgh0
Types of Streams:
● Depending on the type of operations, streams can be divided into
two primary classes:
1. Input Stream: These streams are used to read data that must be taken as an
input from a source array or file or any peripheral device. For eg.,
FileInputStream, BufferedInputStream, ByteArrayInputStream etc.
2. Output Stream: These streams are used to write data as outputs into an array or
file or any output peripheral device. For eg., FileOutputStream,
BufferedOutputStream, ByteArrayOutputStream etc.
ByteStream: This is used to process data byte by byte (8 bits). Though it has
many classes, the FileInputStream and the FileOutputStream are the most
popular ones. The FileInputStream is used to read from the source and
FileOutputStream is used to write to the destination. Here is the list of various
ByteStream Classes:
import java.io.*;
public class Main{
public static void main(String args[])throws Exception{
import java.io.*;
public class Main{
public static void main(String args[])throws Exception{
String name="";
while(!name.equals("stop")){
System.out.println("Enter data: ");
name=bfr.readLine();
System.out.println("data is: "+name);
}
bfr.close();
isr.close();
}
}