Java uses Streams to perform I/O. Java I/O system links the streams to physical devices.For any I/O devices the bhaviour of stream is same. This means that the programmer does not need to specify difference between keyboard and monitor in coding . Java defines two types of streams.
- Byte streams
- Character streams
Byte streams provide a convenient way to handle input or output of bytes, that is, when reading or writing binary data. In Java, Byte stream are defined using two classes InputStream and OutputStream. The two most important methods that are, defined by this class, used for reading and writing of byte are :read() and write().
Character streams provide a convenient way to handle input or output of characters. In Java, Character stream are defined using two classes: Reader and Writer. These classes handle Unicode character streams. The two most important key methods that are, defined by this class, used for reading and writing of character are :read() and write().
Character streams provide a convenient way to handle input or output of characters. In Java, Character stream are defined using two classes: Reader and Writer. These classes handle Unicode character streams. The two most important key methods that are, defined by this class, used for reading and writing of character are :read() and write().
To use or to perform input, the above stream classes must be imported. So you must import java.io..
There is package which is automatically imported to any Java program, it is java.lang.. This package defines System class. the System class has three member variables in,out and err. By accessing these member, helps to perform various I/O. For example System.out.print(); lets to write on console. The member in,out and err are predefined stream variable of System.
The most common method of reading from console is using Character stream. For console input use System.in. To get the character based stream from the console use object of BufferedReader (BufferedReader is a class of Charater stream I/O class). InputStreamReader, which is a subclass of Reader, used to convert bytes to character. This because System.in is an object of type InputStream(byte stream class).
By putting all together the syntax to read from keyboard is:
BufferedReader consolRead= new BufferedReader(new InputStreamReader(System.in));
Here, consolRead is a character based stream to console
Sample program:
import java.io.*;
class Sample{
public static void main(String arg[])throws IOException
{
int a,b,c;
String s;
BufferedReader consolRead= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Your name");
s=consolRead.readLine();
System.out.print("Enter first number: ");
a=Integer.parseInt(consolRead.readLine());
System.out.print("Enter second number: ");
b=Integer.parseInt(consolRead.readLine());
c=a+b;
System.out.println("Sum of "+a+" and "+b+" is = " +c);
}
}
readLine() is used to read a string from the keyboard. It is a member of BufferedReader class. So the streams from console is a stream of character, that is, string . There need a parsing methods to parse string read using readLine(). Java provides parsing methods for parsing into byte, short,integer,long,float,double types. The classes that provide the methods fr parsing are Byte, Short,Integer, Long, Float, Double. For example to parse integer use Integer.parseInt(). The below table shows type and the corresponding methods. It is clear that to read a string there is no need for any parsing methods.
type | parsing |
---|---|
int | Integer.parseInt() |
float | Float.parseFloat() |
short | Short.parseShort() |
double | Double.parseDouble() |
Byte | Byte.parseByte() |
Sample program( Try yourself)
import java.io.*;
class Sample1{
public static void main(String arg[])throws IOException
{
BufferedReader consolRead= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Your name");
String s=consolRead.readLine();
System.out.print("Enter a number: ");
int a=Integer.parseInt(consolRead.readLine());
System.out.print("Enter a float number: ");
double b=Double.parseDouble(consolRead.readLine());
System.out.print("Enter a character: ");
char c=(char)consolRead.read();
//Output
System.out.println("*-- After entering values --*");
System.out.println("Entered integer: "+a);
System.out.println("Entered double: "+b);
System.out.println("Entered string: "+s);
System.out.println("Entered character: "+c);
}
}
Similliarly we can use
DataInputStream consolRead=new DataInputStream(System.in);
to read from keyboard. This also defined in java.io.. Sample program( Try yourself)
import java.io.*;
class Sample2{
public static void main(String arg[])throws IOException
{
DataInputStream consolRead=new DataInputStream(System.in);
System.out.println("Enter Your name");
String s=consolRead.readLine();
System.out.print("Enter a number: ");
int a=Integer.parseInt(consolRead.readLine());
System.out.print("Enter a float number: ");
double b=Double.parseDouble(consolRead.readLine());
System.out.print("Enter a character: ");
char c=(char)consolRead.read();
//Output
System.out.println("*-- After entering values --*");
System.out.println("Entered integer: "+a);
System.out.println("Entered double: "+b);
System.out.println("Entered string: "+s);
System.out.println("Entered character: "+c);
}
}
There is new technique which more easy to read from keyboard, and make coding more simple. The class is Scanner which is defined in java.util. package. the syntax to read from console is:
Scanner read= new Scanner(System.in);
Sample code:
import java.util.Scanner;
class Sample3{
public static void main(String arg[])
{
int a,c;
float b;
String s;
Scanner read= new Scanner(System.in);
System.out.println("Enter Your name");
s=read.nextLine();
System.out.print("Enter integer number: ");
a=read.nextInt();
System.out.print("Enter float number: ");
b=read.nextFloat();
//Output
System.out.println("*-- After entering values --*");
System.out.println("Entered integer: "+a);
System.out.println("Entered float: "+b);
System.out.println("Entered string: "+s);
}
}
type | parsing |
---|---|
Stirng | next(),nextLine() |
int | nextInt() |
float | nextFloat() |
double | nextDouble() |
Byte | nextByte() |
short | nextShort() |
long | nextLong() |
Thank you for visiting
No comments:
Post a Comment